以编程方式更改应用程序语言 - UWP

Change the app language programmatically - UWP

我已经为法语和英语创建了资源文件 (.resw)。现在我想在加载我的应用程序的第一页时调用 "fr" 的资源文件。我做了如下。但是显示异常

"System.NullReferenceException: Object reference not set to an instance of an object".

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 导航。

您很可能会调用更改语言以响应用户操作(例如按钮单击),这时 none 将不再是一个问题并且 Frame 将被定义。

更新

最佳解决方案是在 App.xaml.cs:

OnLaunched 处理程序中设置语言覆盖
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();
    }
}