C# 以编程方式更改应用程序语言 UWP 实时
C# change app language programmatically UWP realtime
在我的应用程序中,每种语言的字符串资源都是单独存储的,并根据语言环境的类型显示。我想更改应用程序设置中的语言。我如何实现在选择语言后立即将其应用到用户界面中?
我们可以使用 ApplicationLanguages.PrimaryLanguageOverride 在运行时更改语言而无需重新启动应用程序。
例如:我支持两种语言 "en" 和 "fr",本地化消息将显示在文本块中。
添加使用 Windows.Globalization;
通过
将默认语言从"en"更改为"fr"
ApplicationLanguages.PrimaryLanguageOverride = "fr";
重新导航到当前页面刷新UI。
Frame.Navigate(this.GetType());
请注意,您需要将 PrimaryLanguageOverride 与系统文化进行比较,以设置下次应用启动的语言,因为 PrimaryLanguageOverride 设置是持久化的。如果您启用了页面缓存,当您动态应用不同的语言时,您需要先设置 Frame.CacheSize = 0;
来清除缓存,然后再将其设置回来。
有一个 page MSDN 描述了 Windows 8.1 中有关语言的新羽毛。
修改后
ApplicationLanguages.PrimaryLanguageOverride = "en-US";
我观看了 属性 resourceContext.Languages[0]
以启动我的 LanguageManager 的 PropertyChanged
事件,这是在 App.xaml
中声明的 StaticResources
x:Key = Loc
.
private void ButtonEn_OnClick(object sender, RoutedEventArgs e)
{
ApplicationLanguages.PrimaryLanguageOverride = "en-US";
UpdateLang("en-US");
}
private async void UpdateLang(string newLang)
{
var resourceContext = Windows.ApplicationModel.Resources.Core.ResourceContext.GetForCurrentView();
while (true)
{
if (resourceContext.Languages[0] == newLang)
{
var loc = Application.Current.Resources["Loc"] as LanguagesManager;
loc.ChangeLang();
break;
}
await Task.Delay(100);
}
}
while (true)
只是为了测试,其实最好用"backup"转义,因为
Those requirements may vary depending on the UI framework used by the app, and it may be necessary to restart the app.
@ThisWillDoIt 和@Herdo
我添加了延迟,以便 "First" 时间适用于我的情况:
Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride = languageCode;
await Task.Delay(100);
Frame.Navigate(this.GetType());
希望对您有所帮助。
Alan Yao 的回答的一些补充。缺少一件:
在你设置 Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride
之后,在重新导航到当前页面之前,你必须调用这两个函数:
Windows.ApplicationModel.Resources.Core.ResourceContext.GetForCurrentView().Reset();
Windows.ApplicationModel.Resources.Core.ResourceContext.GetForViewIndependentUse().Reset();
这样您就不需要 Michael Woolsey 提到的 Task.Delay()
解决方法。
还有一个更重要的最后一步:在创建商店包时,您应该确保将 "Generate app bundle" 设置值设置为 "Never"。来自 this article 的原因:
Because otherwise, it will create a bundle. It means that he will cut
your application into different parts to optimize the download. Only
the parts that are relevant for the devices will be downloaded. For
example, if there are the assets in different resolution, it will only
download the ones that are suitable for the device. Same thing for
languages, it will only download the resources file relevant to the
language of the device. So if you try to change language, it will fall
still fall back on the same base language, because others are not
installed.
不幸的是,如果将页面的 NavigationCacheMode
设置为 "Required"
,则上述答案中的 none 会有所帮助。这是解决我问题的代码。
ApplicationLanguages.PrimaryLanguageOverride = language;
await Task.Delay(300);
Frame rootFrame = Window.Current.Content as Frame;
rootFrame.Content = null;
rootFrame = null;
rootFrame = new Frame();
rootFrame.Navigate(typeof(MainPage), null);
Window.Current.Content = rootFrame;
在我的应用程序中,每种语言的字符串资源都是单独存储的,并根据语言环境的类型显示。我想更改应用程序设置中的语言。我如何实现在选择语言后立即将其应用到用户界面中?
我们可以使用 ApplicationLanguages.PrimaryLanguageOverride 在运行时更改语言而无需重新启动应用程序。
例如:我支持两种语言 "en" 和 "fr",本地化消息将显示在文本块中。
添加使用 Windows.Globalization;
通过
将默认语言从"en"更改为"fr"ApplicationLanguages.PrimaryLanguageOverride = "fr";
重新导航到当前页面刷新UI。
Frame.Navigate(this.GetType());
请注意,您需要将 PrimaryLanguageOverride 与系统文化进行比较,以设置下次应用启动的语言,因为 PrimaryLanguageOverride 设置是持久化的。如果您启用了页面缓存,当您动态应用不同的语言时,您需要先设置 Frame.CacheSize = 0;
来清除缓存,然后再将其设置回来。
有一个 page MSDN 描述了 Windows 8.1 中有关语言的新羽毛。
修改后
ApplicationLanguages.PrimaryLanguageOverride = "en-US";
我观看了 属性 resourceContext.Languages[0]
以启动我的 LanguageManager 的 PropertyChanged
事件,这是在 App.xaml
中声明的 StaticResources
x:Key = Loc
.
private void ButtonEn_OnClick(object sender, RoutedEventArgs e)
{
ApplicationLanguages.PrimaryLanguageOverride = "en-US";
UpdateLang("en-US");
}
private async void UpdateLang(string newLang)
{
var resourceContext = Windows.ApplicationModel.Resources.Core.ResourceContext.GetForCurrentView();
while (true)
{
if (resourceContext.Languages[0] == newLang)
{
var loc = Application.Current.Resources["Loc"] as LanguagesManager;
loc.ChangeLang();
break;
}
await Task.Delay(100);
}
}
while (true)
只是为了测试,其实最好用"backup"转义,因为
Those requirements may vary depending on the UI framework used by the app, and it may be necessary to restart the app.
@ThisWillDoIt 和@Herdo
我添加了延迟,以便 "First" 时间适用于我的情况:
Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride = languageCode;
await Task.Delay(100);
Frame.Navigate(this.GetType());
希望对您有所帮助。
Alan Yao 的回答的一些补充。缺少一件:
在你设置 Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride
之后,在重新导航到当前页面之前,你必须调用这两个函数:
Windows.ApplicationModel.Resources.Core.ResourceContext.GetForCurrentView().Reset();
Windows.ApplicationModel.Resources.Core.ResourceContext.GetForViewIndependentUse().Reset();
这样您就不需要 Michael Woolsey 提到的 Task.Delay()
解决方法。
还有一个更重要的最后一步:在创建商店包时,您应该确保将 "Generate app bundle" 设置值设置为 "Never"。来自 this article 的原因:
Because otherwise, it will create a bundle. It means that he will cut your application into different parts to optimize the download. Only the parts that are relevant for the devices will be downloaded. For example, if there are the assets in different resolution, it will only download the ones that are suitable for the device. Same thing for languages, it will only download the resources file relevant to the language of the device. So if you try to change language, it will fall still fall back on the same base language, because others are not installed.
不幸的是,如果将页面的 NavigationCacheMode
设置为 "Required"
,则上述答案中的 none 会有所帮助。这是解决我问题的代码。
ApplicationLanguages.PrimaryLanguageOverride = language;
await Task.Delay(300);
Frame rootFrame = Window.Current.Content as Frame;
rootFrame.Content = null;
rootFrame = null;
rootFrame = new Frame();
rootFrame.Navigate(typeof(MainPage), null);
Window.Current.Content = rootFrame;