首页 > 解决方案 > 覆盖语言后更改对话框的语言?

问题描述

我在 MainPage.cs 中的代码

ApplicationLanguages.PrimaryLanguageOverride = "ja-jp";

XAML

<Button content="Click" Click="Button_Click" />

在此之后,我用我的代码打开了对话框

 private async void Button_Click(object sender, RoutedEventArgs e)
        {
            {

                testDialog dialog = new testDialog();      
                await dialog.ShowAsync();

            }
        }

我的 testDialog 的 XAML 代码

 <TextBlock x:Uid="TestTextBlock" />

我已经在 Resources.resw 文件中定义了语言的文本,如果我将文本块放在当前的 MainPage 中它工作正常,但是当我把它放在对话框中时,文本块的文本不会改变,它只会在我重置应用程序后改变。有什么想法可以解决这个问题吗?

标签: c#uwpglobalization

解决方案


设置新语言:

Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride = "ja-jp";

Windows.ApplicationModel.Resources.Core.ResourceContext.GetForCurrentView().Reset();
Windows.ApplicationModel.Resources.Core.ResourceContext.GetForViewIndependentUse().Reset();

重新加载当前页面:

 private bool Reload(object param = null)
{
    var type = Frame.CurrentSourcePageType;

    try
    {
        return Frame.Navigate(type, param);
    }
    finally
     {
        Frame.BackStack.Remove(Frame.BackStack.Last());
     }

}

您也可以Frame.Navigate(this.GetType());用于刷新当前页面 UI。
请查看此帖子以获取更多信息:动态更改通用应用程序的语言


推荐阅读