【问题标题】:How to properly change a resource dictionary如何正确更改资源字典
【发布时间】:2013-07-09 15:55:57
【问题描述】:

我正在致力于在 WPF 中本地化 GUI。我在这里发布的代码将来自原型,因为我试图让这个问题尽可能简单。

我正在使用资源字典来设置窗口中字符串的内容。我有两个,一个是德语,一个是英语。

德语资源词典:

<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 窗口中动态设置按钮的内容:

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

我希望在单击英语按钮时将语言更改为英语,并在单击德语按钮时更改为德语。我已经做到了这一点,但我不知道告诉程序更改它的资源字典所需的实际代码。我猜这会在两个按钮的单击事件下隐藏在代码中,并且可能在 XAML 窗口中也有一些代码。

这些是我使用过的资源,但我发布这个问题是因为最终解决方案对我来说不够清楚:

-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

感谢您的帮助!

【问题讨论】:

    标签: wpf xaml localization resourcedictionary


    【解决方案1】:

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

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

    如果您想将语言更改为德语,您应该在第二行更改文件名。

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

    检查我在这个问题中的答案:

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

    【讨论】:

    • 哦,太棒了....我想通了。感谢您的帮助,从昨天早上开始我一直在努力解决这个问题!
    【解决方案2】:

    试试Wpf Localize Extension,它支持在运行时更改语言

    【讨论】:

      【解决方案3】:

      将资源字典添加到 App.xaml。 按钮点击设置

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

      我建议使用 RESX 文件进行本地化。

      【讨论】:

      • 我的资源字典路径会是什么,如果它只是在 App.xaml 文件中,就像你说的那样?我尝试使用 RESX 文件进行本地化,但我不知道如何在运行时使用按钮单击事件更改资源文件。
      • 最初,您将在 App.xaml 中添加默认语言词典,然后单击按钮,您将使用所需的语言词典替换该词典。因此,假设您将语言资源字典放在 Resource 文件夹中,那么路径将是 new Uri("Resources/German.xaml", UriKind.Relative);
      • 这是有道理的。上面的代码在按钮点击事件中?
      • 要使用 RESX 文件实现,假设您要实现英语和法语两种语言。因此,添加两个名为 Resources.resx(默认为英文)和 Resources.fr-FR.resx 的 resx 文件。然后在这些文件中添加您的资源(特定于语言的单词)。然后在 XAML 中添加命名空间 xmlns:properties="clr-namespace:YourProjectName.Properties"
      • 我已经做到了,但这不支持语言的运行时更改。
      【解决方案4】:

      这对我有用

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多