UWP - Change of Languages

浪子不回头ぞ 提交于 2020-01-24 10:59:05

问题


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());

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).

I would really appreciate a solution or an explanation that allows good functionality that is not based on delaying threads.

Some other related Q&As:

UWP MVVM: refresh page after change of language

C# change app language programmatically UWP realtime

Thanks


回答1:


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());               
}


来源:https://stackoverflow.com/questions/54789409/uwp-change-of-languages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!