0

我使用 MergedDictionaries 和项目的设置在我的项目中编写了一个选择语言选项。问题是语言仅在我的 MainWindow 中成功更改,而在其他 Windows中也没有。我究竟做错了什么?
MainWindow 中的设置语言功能(编辑:MainWindow.cs):

/*set language*/
    private void SetLanguageDictionary()
    {
        ResourceDictionary dict = new ResourceDictionary();
        if (Properties.Settings.Default.Language.Equals("en")) //english was set
        {
            dict.Source = new Uri("\\res\\enDictionary.xaml", UriKind.Relative);
        }
        else //otherwise - hebrew as default lang.
        {
            dict.Source = new Uri("\\res\\hebDictionary.xaml", UriKind.Relative);
        }
        //add required dictionary to the MergedDictionaries
        Resources.MergedDictionaries.Add(dict);
    }

一个字典的一个小例子[它们是对称设置的,如果重要的话]:

<ResourceDictionary 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:UI_WPF"
xmlns:system="clr-namespace:System;assembly=mscorlib">
<system:String x:Key="employees">Employees</system:String>
<system:String x:Key="employers">Employers</system:String>
<system:String x:Key="contracts">Contracts</system:String> </ResourceDictionary>
4

2 回答 2

1

您还没有告诉我们 SetLanguageDictionary() 方法的定义位置,但如果您想全局应用资源,您可以将 ResourceDictionary 合并到全局 Application.Current.Resources 中:

Application.Current.Resources.MergedDictionaries.Add(dict);
于 2017-01-29T18:32:30.437 回答
1

你知道为什么只在 MainWindow 中改变语言吗?因为当你调用时 SetLanguageDictionary()只有 MainWindow 会刷新(重新加载),这就是为什么标签和文本会改变。为了在其他窗口中更改语言,您需要刷新它们 - 再次重新加载它们 - 在重新加载过程中,内容和标签将被更新。

您可以从 MainWindow 调用其他窗口,如下所示

window win = new window();
//then
win.AnyMethodyou_want();

new window()将再次重新加载窗口,然后可以更改语言。

我以前用过这种方法。。

于 2017-01-29T19:02:05.767 回答