How to properly change a resource dictionary

做~自己de王妃 提交于 2019-12-12 15:44:30

问题


I am working on localizing a GUI in WPF. The code that I post here will be from a prototype because I am trying to make this question as simple as possible.

I am using Resource Dictionaries to set the content of the strings in my window. I have two of them, one is German, and the other is English.

German Resource Dictionary:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:sys="clr-namespace:System;assembly=mscorlib">

    <!-- The name of this ResourceDictionary. Should not be localized. -->
    <sys:String x:Key="ResourceDictionaryName" Localization.Comments="$Content(DoNotLocalize)">Loc-de-DE</sys:String>

    <!--- Button -->
    <sys:String x:Key="MainButton_Title">Hallo Welt!</sys:String>
    <sys:String x:Key="EnglishButton">Englisch</sys:String>
    <sys:String x:Key="GermanButton">Deutsch</sys:String>

</ResourceDictionary>

English Resource Dictionary:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:sys="clr-namespace:System;assembly=mscorlib">

    <!-- The name of this ResourceDictionary. Should not be localized. -->
    <sys:String x:Key="ResourceDictionaryName" Localization.Comments="$Content(DoNotLocalize)">Loc-en-US</sys:String>

    <!--- Buttons -->
    <sys:String x:Key="MainButton_Title">Hello World!</sys:String>
    <sys:String x:Key="EnglishButton">English</sys:String>
    <sys:String x:Key="GermanButton">German</sys:String>

</ResourceDictionary>

I also have dynamically set the content of my buttons in the XAML window:

<Button Content="{DynamicResource MainButton_Title}" Margin="59,68,0,80" Name="button1" HorizontalAlignment="Left" Width="152" />
<Button Content="{DynamicResource EnglishButton}" Height="23" HorizontalAlignment="Right" Margin="0,83,87,0" Name="button2" VerticalAlignment="Top" Width="75" Click="button2_Click" />
<Button Content="{DynamicResource GermanButton}" Height="23" HorizontalAlignment="Right" Margin="0,0,87,90" Name="button3" VerticalAlignment="Bottom" Width="75" />

I want the language to change to English when I click the English button, and change to German when I click the German button. I have gotten this far, but what I don't know is the actual code that would be needed to tell the program to change it's Resource Dictionary. I'm guessing this would go in code-behind under the two button's click events, and might also have some code in the XAML window.

These are the resources that I've used, but I am posting this question because the end solution is not clear enough to me:

-http://www.wpfsharp.com/2012/01/27/how-to-change-language-at-run-time-in-wpf-with-loadable-resource-dictionaries-and-dynamicresource-binding/

-http://msdn.microsoft.com/en-us/library/bb613547.aspx

-http://blogs.msdn.com/b/wpfsldesigner/archive/2010/06/03/creating-and-consuming-resource-dictionaries-in-wpf-and-silverlight.aspx

Thank you for your help!


回答1:


You should create new resource dictionary and after that you should merge with current dictionary:

ResourceDictionary dict = new ResourceDictionary();
dict.Source = new Uri("..\\Languages\\EnglishLang.xaml", UriKind.Relative);
this.Resources.MergedDictionaries.Add(dict);

If you want change language to German you should change file name in the second line.

In my example your resource dictionary should be in Languages folder.

Check my answer in this question:

How to start an internationalized WPF-Project in SharpDevelop 4.2?




回答2:


Try Wpf Localize Extension which support language change at runtime




回答3:


Add resource dictionaries to App.xaml. On button click set

    ResourceDictionary _objRD = new ResourceDictionary();
    _objRD.Source = new Uri("Your Resource Dictionary Path", UriKind.Relative);
    App.Current.Resources.MergedDictionaries[0] = _objRD;

I would recommend localization using RESX file.




回答4:


This worked for me

        ResourceDictionary dict = new ResourceDictionary();
        dict.Source = new Uri("Lang.pl-PL.xaml", UriKind.Relative);
        App.Current.Resources.MergedDictionaries[0] = dict;


来源:https://stackoverflow.com/questions/17552804/how-to-properly-change-a-resource-dictionary

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!