C# change app language programmatically UWP realtime(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.
使用 Windows.Globalization 添加;
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 实时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:C# 以编程方式更改应用程序语言 UWP 实时
基础教程推荐
- 将 XML 转换为通用列表 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 如何激活MC67中的红灯 2022-01-01