2017-01-29 31 views
0

我已经使用MergedDictionaries和项目的设置在我的项目中编写了一个选择语言选项。问题是语言只在我的主窗口中成功更改,而其他Windows也不成功。我究竟做错了什么?在主窗口
设置语言FUNC(编辑:MainWindow.cs):WPF - 仅在MainWindow中更改语言?

/*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> 
+0

没有足够的信息,如果字典不添加其无法使用它。这意味着你正在合并导致这个问题的英文字典。 –

回答

1

您还没有告诉我们在哪里定义了SetLanguageDictionary()方法,但是如果要全局应用资源,则可以将ResourceDictionary合并到全局Application.Current.Resources中:

Application.Current.Resources.MergedDictionaries.Add(dict); 
+0

就是这样!谢谢[和其他谁也帮忙!],SetLanguageDictionary()被设置在MainWindow.cs中(被编辑),并且整个应用程序需要受到影响。 – Yair

1

你知道为什么只在MainWindow中改变语言吗?因为当你打电话给SetLanguageDictionary()时,只有MainWindow会刷新(重新加载),以及为什么标签和文本会改变。为了在其他窗口中更改语言,您需要刷新它们 - 重新加载它们 - 并且在重新加载过程中,内容和标签将被更新。

你可以从主窗口调用其他窗口,如下图所示

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

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

我已经使用过这样的感觉..