C# 以编程方式更改应用程序语言 UWP 实时

C# change app language programmatically UWP realtime(C# 以编程方式更改应用程序语言 UWP 实时)

本文介绍了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 实时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

    本文标题为:C# 以编程方式更改应用程序语言 UWP 实时

    基础教程推荐