如何正确更改资源字典 [英] How to properly change a resource dictionary

查看:128
本文介绍了如何正确更改资源字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在本地化WPF中的GUI.我在此处发布的代码将来自原型,因为我试图使这个问题尽可能简单.

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.

德语资源词典:

<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>

英语资源词典:

<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>

我还已经在XAML窗口中动态设置了按钮的内容:

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" />

我希望在单击英语按钮时将语言更改为英语,而在单击德语按钮时将语言更改为德语.到目前为止,我还不知道告诉程序更改它的实际代码是资源字典.我猜想这会在两个按钮的click事件下的代码后面,并且在XAML窗口中也可能有一些代码.

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://msdn.microsoft.com/en-us/library /bb613547.aspx

-谢谢您的帮助!

推荐答案

您应该创建新的资源字典,然后再与当前字典合并:

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.

在我的示例中,您的资源字典应位于Languages文件夹中.

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

检查我在以下问题中的答案:

Check my answer in this question:

如何开始是在SharpDevelop 4.2中进行国际化的WPF项目?

这篇关于如何正确更改资源字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆