2

我使用众所周知的LocBaml方法来改变文化。

它在这里工作正常:

public App()
{ 
    // Test code 
    bool override_current_ui_language = true;
    string locale = "es-ES";
    if (override_current_ui_language)
    {
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(locale);
        Thread.CurrentThread.CurrentCulture = new CultureInfo(locale);
    }    
} 

但是当我在 WPF Window 类控制器下使用它时它不起作用。

任何线索为什么?


我使用它,但效果不佳。

void cmbLanguages_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            string locale = "es-ES";
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(locale);
            Thread.CurrentThread.CurrentCulture = new CultureInfo(locale);
        }
4

2 回答 2

1

为此,我使用LocalizeExtension

在 .xaml 中,您只需用于{LocText NAMESPACE::RESOURCENAME}设置文本,并在以下代码中即时更改语言:

Thread.CurrentThread.CurrentCulture = culture;
Thread.CurrentThread.CurrentUICulture = culture;
LocalizeDictionary.Instance.Culture = culture;

您可以在我使用它的 Codeplex 上查看我的项目,以查看扩展示例:
XAMLCodeBehind (->SetUICulture)

于 2012-01-20T12:22:10.807 回答
1

如果您在调用之前执行此操作,它应该在构造函数InitalizeComponent()中工作,但您的示例显示了一个事件处理程序。一旦InitializeComponent()被调用,BAML 就已从资源中加载,您将无法更改它。

您可以使用此处说明的方法https://www.codeproject.com/articles/29800/webcontrols/使用您的新文化创建一个新窗口:

void SwitchCulture(CultureInfo newCulture)
{
    Thread.CurrentThread.CurrentUICulture = newCulture;
    Thread.CurrentThread.CurrentCulture = newCulture;
    // Reload all the merged dictionaries to reset the resources.
    List<Uri> dictionaryList = new List<Uri>();
    foreach (ResourceDictionary dictionary in Application.Current.Resources.MergedDictionaries)
    {
        dictionaryList.Add(dictionary.Source);
    }
    Application.Current.Resources.MergedDictionaries.Clear();
    foreach (Uri uri in dictionaryList)
    {
        ResourceDictionary resourceDictionary1 = new ResourceDictionary();
        resourceDictionary1.Source = uri;
        Application.Current.Resources.MergedDictionaries.Add( resourceDictionary1 );
    }

    MyWindowClass newWindow = new MyWiondowClass();
    // TODO: Attach any view model so the new window looks like the old one
    newWindow.Show();
    this.Close();
} 
于 2018-07-14T09:32:39.733 回答