WPF语言资源选择组合框无法正常工作

时间:2017-03-08 17:02:59

标签: c# wpf xaml combobox

我正在使用一个带有2种语言选择的ComboBox的WPF C#应用程序。我发现在线教程完全符合我想要的语言资源,但问题是我有几个WPF窗口,所有这些窗口都有一个按钮链接到主窗口,当应用程序被触发时,ComboBox就会启动是可用的。首次启动默认为英语时,语言选择工作正常,但是当您使用不同语言从主窗口导航到另一个窗口时出现问题,当单击主页按钮时,所选语言始终为英语,尽管我已将其更改为其他语言语言,如果我需要改回英语,我需要点击另一种语言,然后点击返回英语,恢复为英语: 如何使应用程序自动检测当前资源字典并更改组合框以反映所选语言? 这是一些代码:

private void ddlLanguage_SelectionChanged(object sender, SelectionChangedEventArgs e)  
{  
    var currentResourceDictionary = (
        from d in BaseModel.Instance.ImportCatalog.ResourceDictionaryList  
        where d.Metadata.ContainsKey("Culture") && d.Metadata["Culture"].ToString().Equals(vm.SelectedLanguage.Code)  
        select d).FirstOrDefault();

    if (currentResourceDictionary != null)  
    {  
        var previousResourceDictionary = (
            from d in BaseModel.Instance.ImportCatalog.ResourceDictionaryList  
            where d.Metadata.ContainsKey("Culture") && d.Metadata["Culture"].ToString().Equals(vm.PreviousLanguage.Code)  
            select d).FirstOrDefault();

        if (previousResourceDictionary != null && previousResourceDictionary != currentResourceDictionary)
        {
            Application.Current.Resources.MergedDictionaries.Remove(previousResourceDictionary.Value);
            Application.Current.Resources.MergedDictionaries.Add(currentResourceDictionary.Value);

            CultureInfo cultureInfo = new CultureInfo(vm.SelectedLanguage.Code);

            Thread.CurrentThread.CurrentCulture = cultureInfo;
            Thread.CurrentThread.CurrentUICulture = cultureInfo;

            Application.Current.MainWindow.Language = XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag);

            vm.PreviousLanguage = vm.SelectedLanguage;
        }
    }
}  

在ViewModel构造函数中:

public MainViewModel()
{
    LoadResources();
    SelectedLanguage = LanguageList.FirstOrDefault();
    PreviousLanguage = SelectedLanguage;
}

private void LoadResources()
{
    LanguageList = new List<Languages>();
    LanguageList.Add(new Languages() { Code = "en-GB", Name = "English" });
    LanguageList.Add(new Languages() { Code = "cy-GB", Name = "Cymraeg" });
}

在MainWindow构造函数中:

MainViewModel vm;  

public MainMenu()  
{  
    InitializeComponent();
    vm = new MainViewModel();
    this.DataContext = vm;
}

在ComboBox的MainWindow xaml中:

<ComboBox x:Name="ddlLanguage" 
          FontSize="22"
          DisplayMemberPath="Name"
          ItemsSource="{Binding LanguageList}"
          SelectedValue="{Binding SelectedLanguage, Mode=TwoWay}"
          SelectionChanged="ddlLanguage_SelectionChanged"
          Margin="178,61,940,148" />

2 个答案:

答案 0 :(得分:1)

我已经找到了解决我的问题可能看起来不寻常:我已经从ViewModel主方法启动了语言列表,并删除了ComboBox的启动,并留下了一个空白条目:

public TestViewModel()
{
    language = new List<Languages>
    {
        new Languages { Name="English", Code="en-GB" },
        new Languages { Name="Welsh", Code="cy-GB" },
    };
    //SelectedLanguage = language.FirstOrDefault();
    //PreviousLanguage = selectedLanguage;
}

在我的ddlLanguage_SelectionChanged方法中,我执行以下操作(它可能不是强大的编码,但它现在已经有效):

private void ddlLanguage_SelectionChanged(object sender, RoutedEventArgs e)
{
    #region Method body
    var indx = ddlLanguage.ItemsSource;
    string previousLang = "";

    foreach (var item in indx)
    {
        switch (ddlLanguage.SelectedIndex)
        {
            case 0:
                ddlLanguage.SelectedItem = "English";
                vm.SelectedLanguage.Code = "en-GB";
                previousLang = "cy-GB";

                var currentResourceDictionary = (
                    from d in BaseModel.Instance.ImportCatalog.ResourceDictionaryList
                    where d.Metadata.ContainsKey("Culture") && d.Metadata["Culture"].ToString().Equals(vm.SelectedLanguage.Code)
                    select d).SingleOrDefault();

                if (currentResourceDictionary != null)
                {
                    var previousResourceDictionaryE = (
                        from d in BaseModel.Instance.ImportCatalog.ResourceDictionaryList
                        where d.Metadata.ContainsKey("Culture") && d.Metadata["Culture"].ToString().Equals(previousLang)
                        select d).SingleOrDefault();

                    if (previousResourceDictionaryE != null && previousResourceDictionaryE != currentResourceDictionary)
                    {
                        Application.Current.Resources.MergedDictionaries.Add(currentResourceDictionary.Value);
                        CultureInfo cultureInfo = new CultureInfo(vm.SelectedLanguage.Code);
                        Thread.CurrentThread.CurrentCulture = cultureInfo;
                        Thread.CurrentThread.CurrentUICulture = cultureInfo;
                        Application.Current.MainWindow.Language = XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag);
                        previousLang = vm.SelectedLanguage.Code;
                    }
                }
                break;

            case 1:
                ddlLanguage.SelectedItem = "Welsh";

                vm.SelectedLanguage.Code = "cy-GB";
                previousLang = "en-GB";
                var currentResourceDictionaryE = (
                    from d in BaseModel.Instance.ImportCatalog.ResourceDictionaryList
                    where d.Metadata.ContainsKey("Culture") && d.Metadata["Culture"].ToString().Equals(vm.SelectedLanguage.Code)
                    select d).SingleOrDefault();
                if (currentResourceDictionaryE != null)
                {
                    if (currentResourceDictionaryE != null)
                    {
                        var previousResourceDictionaryE = (
                            from d in BaseModel.Instance.ImportCatalog.ResourceDictionaryList
                            where d.Metadata.ContainsKey("Culture") && d.Metadata["Culture"].ToString().Equals(previousLang)
                            select d).SingleOrDefault();

                        if (previousResourceDictionaryE != null && previousResourceDictionaryE != currentResourceDictionaryE)
                        {
                            Application.Current.Resources.MergedDictionaries.Add(currentResourceDictionaryE.Value);
                            CultureInfo cultureInfo = new CultureInfo(vm.SelectedLanguage.Code);
                            Thread.CurrentThread.CurrentCulture = cultureInfo;
                            Thread.CurrentThread.CurrentUICulture = cultureInfo;
                            Application.Current.MainWindow.Language = XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag);
                            previousLang = vm.SelectedLanguage.Code;
                        }
                    }
                }    
                break;
            }
        }

        #endregion Method body
    }
}

这种方式当应用程序首次加载ComboBox时没有显示任何条目,但您可以选择所选语言,当单击返回应用程序的主页面时,相同的进程以这种方式启动,用户不会对当前显示的语言和ComboBox的选定值,它始终引用FirstOrDefault()值。 我设法使用名为ItemSource的ComboBox属性,它是基于索引的,然后使用switch语句来应用我的逻辑。

如果您看到我可以申请的任何修改,请随时发表评论。

答案 1 :(得分:0)

首先尝试在设置或数据库中保存所选语言 当应用程序开始使用保存的选定语言更改语言时,并记下一条说明:必须重新启动应用程序才能应用更改 。并检查您在绑定中使用DynamicResource

 <TextBlock Text="{DynamicResource myText}"/>