UWP App Language不会动态变化

时间:2018-01-05 07:12:48

标签: c# uwp resources globalization

我编写了代码,可以在按钮点击中将文本块的语言更改为其他语言。但点击按钮时不会改变。但是当我重新启动应用程序或者再次单击按钮时似乎会改变。如何动态更改语言?

    private void Btn_Click(object sender, RoutedEventArgs e)
    {
        ChangeLanguage("fr");
    }
    private void ChangeLanguage(string language)
    {
        var culture = new System.Globalization.CultureInfo(language);
        Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride = culture.Name;
        Windows.ApplicationModel.Resources.Core.ResourceContext.GetForCurrentView().Reset();
        Windows.ApplicationModel.Resources.Core.ResourceContext.GetForViewIndependentUse().Reset();
        if (Frame != null)
        Frame.Navigate(typeof(MainPage));
    }

1 个答案:

答案 0 :(得分:0)

您可以动态更改语言:

        var resourceContext = ResourceContext.GetForCurrentView();
        var resourceMap = ResourceManager.Current.MainResourceMap.GetSubtree("Resources");
        resourceContext.QualifierValues["Language"] = "fr";
        //if your string in .resw file called "Name.Text" you should swap "." to "/" in code or XAML
        var localizedString = resourceMap.GetValue("Name/Text", resourceContext).ValueAsString;

现在,如果你想要x:Bind,你必须创建一些ObservableCollection本地化项目并使用foreach OnLanguageChange事件或某个实现INotifyPropertyChanged的类来更改它ping所有与语言相关的资源。