本文介绍了C# 以编程方式实时更改应用程序语言 UWP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,每种语言的字符串资源都单独存储,并根据语言环境的类型进行显示.我想在应用程序设置中更改语言.我如何意识到在语言选择后立即将其应用到用户界面中?

In my application for each language string resources are stored separately and are displayed depending of type of language environment. I want to change the language in the application settings. How do I realize that after the language selection instantly apply it in the user interface?

推荐答案

我们可以使用 ApplicationLanguages.PrimaryLanguageOverride 无需重启应用程序即可在运行时更改语言.

We can use ApplicationLanguages.PrimaryLanguageOverride to change the language during runtime without restart the app.

例如:我有两种语言支持en"和fr",本地化的消息将显示在文本块中.

For example: I have two languages supported "en" and "fr", localized message will show up in textblock.

  1. 使用 Windows.Globalization 添加;

  1. Add using Windows.Globalization;

将默认语言从en"更改为fr"

Change the default language from "en" to "fr" by

ApplicationLanguages.PrimaryLanguageOverride = "fr";

  • 重新导航到当前页面以刷新 UI.

  • Re-navigate to the current page to refresh the UI.

    Frame.Navigate(this.GetType());
    

  • 请注意,您需要将 PrimaryLanguageOverride 与系统文化进行比较,以设置下次应用程序启动的语言,因为 PrimaryLanguageOverride 设置已保留.如果您启用了页面缓存,当您即时应用不同的语言时,您需要先设置 Frame.CacheSize = 0; 来清除缓存,然后再将其设置回来.

    Note that, you need to compare the PrimaryLanguageOverride with the system culture to set the language for next app launch, because the PrimaryLanguageOverride setting is persisted. And if you have page cache enabled, when you apply a different language on the fly, you need to clear the cache by setting Frame.CacheSize = 0; first then set it back.

    这篇关于C# 以编程方式实时更改应用程序语言 UWP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    07-29 19:30
    查看更多