以编程方式更改应用程序语言 - 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的主要内容,如果未能解决你的问题,请参考以下文章

如何以编程方式更改 Win 8.1 或 Win 10 UWP 应用的背景主题?

UWP TabView 以编程方式更改选项卡

如何以编程方式设置 UWP 应用的方向

有没有办法以编程方式使用kotlin更改片段中的文本颜色?

以编程方式使用选项卡更改片段的选项卡索引

显示/隐藏片段并以编程方式更改可见性属性