WPF - 仅在 MainWindow 中更改语言?
WPF - language changes only in MainWindow?
我使用 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>
您没有告诉我们在哪里定义了 SetLanguageDictionary() 方法,但是如果您想全局应用资源,您可以将 ResourceDictionary 合并到全局 Application.Current.Resources:
Application.Current.Resources.MergedDictionaries.Add(dict);
你知道为什么语言只在 MainWindow 中改变吗?因为当你调用 SetLanguageDictionary()
只有 MainWindow 会刷新(重新加载),这就是为什么标签和文本会改变的原因。要更改其他 window 中的语言,您需要刷新它们 - 重新加载它们 - 在重新加载过程中,内容和标签将被更新。
您可以从 MainWindow 调用其他 window,如下所示
window win = new window();
//then
win.AnyMethodyou_want();
new window()
将再次加载 window,然后可以更改语言。
我以前用过这种方式..
我使用 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>
您没有告诉我们在哪里定义了 SetLanguageDictionary() 方法,但是如果您想全局应用资源,您可以将 ResourceDictionary 合并到全局 Application.Current.Resources:
Application.Current.Resources.MergedDictionaries.Add(dict);
你知道为什么语言只在 MainWindow 中改变吗?因为当你调用 SetLanguageDictionary()
只有 MainWindow 会刷新(重新加载),这就是为什么标签和文本会改变的原因。要更改其他 window 中的语言,您需要刷新它们 - 重新加载它们 - 在重新加载过程中,内容和标签将被更新。
您可以从 MainWindow 调用其他 window,如下所示
window win = new window();
//then
win.AnyMethodyou_want();
new window()
将再次加载 window,然后可以更改语言。
我以前用过这种方式..