UWP - 语言更改 [英] UWP - Change of Languages

查看:33
本文介绍了UWP - 语言更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更改 UWP 应用程序中的语言,以便我的 x:uid 对象相关和我的基于 ResourceLoader 的对象会更改.我正在按照其他问答中的建议使用:

I am trying to change a language at a UWP app so that the my x:uid objects related and my ResourceLoader based objects would change. I am using, as recommended at other Q&A :

ApplicationLanguages.PrimaryLanguageOverride = newLanguage;
Windows.ApplicationModel.Resources.Core.ResourceContext.GetForCurrentView().Reset();
Windows.ApplicationModel.Resources.Core.ResourceContext.GetForViewIndependentUse().Reset();
Frame.Navigate(this.GetType());

但是,由 ResourceLoader 直接控制的所有内容都已更改,并且在 xaml 中使用 x:uid 创建的内容不会更改.如果我再次更改语言,x:uid 将更改为以前的语言,其余为新选择的语言.

However, everything that is controlled directly by the ResourceLoader is changed and whatever is created at the xaml with x:uid don't. If I change the language again then, x:uid changes to the previous language and the rest is at the new selected language.

我认为可能有一些与线程行为相关的原因有两个.首先,修复它的建议是在导航之前延迟线程.其次,我在虚拟机和物理机上有不同的行为(这个错误显然发生在物理机上).

I think that there might be something that is related to thread behavior for two reasons. First, a suggestion to fix it, that works, is to delay the thread before the navigation. Second, I have different behavior at virtual and physical machines (this bug happens at the physical machine obviously).

我非常感谢能够提供不基于延迟线程的良好功能的解决方案或解释.

其他一些相关问答:

UWP MVVM:刷新页面更改语言后

C# 以编程方式实时 UWP 更改应用语言

谢谢

推荐答案

我已经报告了这个问题,目前有一个解决方法是在调用 Reset 方法后使用延迟.您可以参考以下代码.

I have reported this issue, and currently there is a workaround that use delay after Reset method invoked. You could refer the following code.

using System.Threading.Tasks;

static bool m_bFirstLanguageChangeNavigation = true;


async private void Show_Click(object sender, RoutedEventArgs e)
{
    var context = ResourceContext.GetForCurrentView();
    var selectedLanguage = MyComboBox.SelectedValue;
    var lang = new List<string>();
    lang.Add(selectedLanguage.ToString());
    ApplicationLanguages.PrimaryLanguageOverride = selectedLanguage.ToString();
    ResourceContext.GetForCurrentView().Reset();
    ResourceContext.GetForViewIndependentUse().Reset();


    //added to work the first time
   if (m_bFirstLanguageChangeNavigation)
    {
       m_bFirstLanguageChangeNavigation = false;
       await Task.Delay(100);
    }


    Frame.Navigate(this.GetType());               
}

这篇关于UWP - 语言更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆