【问题标题】:WPF - language changes only in MainWindow?WPF - 仅在 MainWindow 中更改语言?
【发布时间】:2017-06-14 22:21:59
【问题描述】:

我使用 MergedDictionaries 和项目设置在我的项目中编写了选择语言选项。问题是语言仅在我的 MainWindow 中成功更改,而在其他 Windows 中也没有。我做错了什么?
在 MainWindow 中设置语言函数(edit: 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>

【问题讨论】:

  • 没有足够的信息,如果没有添加字典是无法使用的。这意味着您正在合并导致此问题的英语词典。

标签: c# .net wpf dictionary mergeddictionaries


【解决方案1】:

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

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

【讨论】:

  • 就是这样!谢谢[和其他也提供帮助的人!],SetLanguageDictionary() 是在 MainWindow.cs 中设置的(已编辑),需要影响整个应用程序。
【解决方案2】:

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

你可以像下面这样从 MainWindow 调用其他窗口

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

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

我以前用过这种方式..

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-04
    • 2011-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-13
    • 2011-08-30
    相关资源
    最近更新 更多