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

时间:2018-03-01 10:00:53

标签: c# xaml uwp multilingual

我已经为法语和英语创建了资源文件(.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            
        }
    }
 }

1 个答案:

答案 0 :(得分:2)

问题是您在页面内过早地调用该方法。在构造函数中,页面尚未分配给它所在的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();
    }
}