以编程方式更改应用程序语言 - UWP
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了以编程方式更改应用程序语言 - UWP相关的知识,希望对你有一定的参考价值。
我已经为法语和英语创建了资源文件(.resw
)。现在我想在加载我的应用程序的第一页时调用资源文件“fr”。我在下面做了。但它显示了一个例外
“System.NullReferenceException:对象引用未设置为对象的实例”。
XAML
<TextBlock x:Uid="txt_launch3" Grid.Row="4" Padding="7"/>
代码隐藏
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
string getDeviceDefaultLang="fr";
ChangeLanguage2(getDeviceDefaultLang);
}
private void ChangeLanguage2(string language)
{
try
{
ApplicationLanguages.PrimaryLanguageOverride =language;
Frame.CacheSize = 0;
Windows.ApplicationModel.Resources.Core.ResourceContext.GetForCurrentView().Reset(); Windows.ApplicationModel.Resources.Core.ResourceContext.GetForViewIndependentUse().Reset();
Frame.Navigate(this.GetType());
}
catch (Exception ex)
{
string exx = ex.ToString(); //getting System.NullReferenceException
}
}
}
答案
问题是您在页面内过早地调用该方法。在构造函数中,页面尚未分配给它所在的Frame
。因此,Frame
仍然是那里的null
。
您可以将方法调用移动到OnNavigatedTo
覆盖:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
string getDeviceDefaultLang = "fr";
ChangeLanguage2(getDeviceDefaultLang);
}
private void ChangeLanguage2(string language)
{
try
{
ApplicationLanguages.PrimaryLanguageOverride = language;
Frame.CacheSize = 0;
Windows.ApplicationModel.Resources.Core.ResourceContext.GetForCurrentView().Reset();
Windows.ApplicationModel.Resources.Core.ResourceContext.GetForViewIndependentUse().Reset();
Frame.Navigate(this.GetType());
}
catch (Exception ex)
{
string exx = ex.ToString(); //getting System.NullReferenceException
}
}
或者,您可以直接访问应用程序的根框架,而不是通过页面的Frame
属性:
private void ChangeLanguage2(string language)
{
try
{
ApplicationLanguages.PrimaryLanguageOverride = language;
var rootFrame = Window.Current.Content as Frame;
rootFrame.CacheSize = 0;
Windows.ApplicationModel.Resources.Core.ResourceContext.GetForCurrentView().Reset();
Windows.ApplicationModel.Resources.Core.ResourceContext.GetForViewIndependentUse().Reset();
rootFrame.Navigate(this.GetType());
}
catch (Exception ex)
{
string exx = ex.ToString(); //getting System.NullReferenceException
}
}
然而,这实际上并不是最佳的,因为当你正在进行导航时,你实际上是在导航(原始的MainPage
导航。
最有可能的是,无论如何你都会调用更改语言以响应用户操作(例如按钮点击),此时这些都不再是问题,并且将定义Frame
。
更新
最好的解决方案是在OnLaunched
中的App.xaml.cs
处理程序中设置语言覆盖:
protected override void OnLaunched(LaunchActivatedEventArgs e)
{
Frame rootFrame = Window.Current.Content as Frame;
// Do not repeat app initialization when the Window already has content,
// just ensure that the window is active
if (rootFrame == null)
{
// Create a Frame to act as the navigation context and navigate to the first page
rootFrame = new Frame();
rootFrame.NavigationFailed += OnNavigationFailed;
if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)
{
//TODO: Load state from previously suspended application
}
// Place the frame in the current Window
Window.Current.Content = rootFrame;
}
if (e.PrelaunchActivated == false)
{
if (rootFrame.Content == null)
{
ApplicationLanguages.PrimaryLanguageOverride = "fr";
Windows.ApplicationModel.Resources.Core.ResourceContext.GetForCurrentView().Reset();
Windows.ApplicationModel.Resources.Core.ResourceContext.GetForViewIndependentUse().Reset();
// When the navigation stack isn't restored navigate to the first page,
// configuring the new page by passing required information as a navigation
// parameter
rootFrame.Navigate(typeof(MainPage), e.Arguments);
}
// Ensure the current window is active
Window.Current.Activate();
}
}
以上是关于以编程方式更改应用程序语言 - UWP的主要内容,如果未能解决你的问题,请参考以下文章