NavigationViw不会根据PrimaryLanguageOverride更改语言

时间:2019-08-23 15:25:00

标签: c# xaml uwp

很久以前,我回到编程领域,进行了太多更改。 我从template studio开始,创建了3页和设置,其中包含来自MVVM light的Shell页的NavigaionView。 现在,我可以更改主题,添加新语言并保存设置。 更改语言时,除了NavigationView标头和菜单项名称外,其他所有内容在重新加载当前页面后都会更改,仅在重新启动应用程序后才会更改。 谁能帮我如何强制ShellPage的navigationView刷新新的语言设置?

对于SelectionChanged事件:

 private void CmbLanguage_SelectionChanged(object sender, SelectionChangedEventArgs e)

  { string LanguageCode = (sender as ComboBox).SelectedValue as string;
    Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride = LanguageCode;
           Windows.ApplicationModel.Resources.Core.ResourceContext.GetForCurrentView().Reset();
            Windows.ApplicationModel.Resources.Core.ResourceContext.GetForViewIndependentUse().Reset();
Frame.Navigate(this.GetType());

 }

这是用于从ComboBox进行绑定的:

private Language _selectedLanguage;
public Language SelectedLanguage

{        
   get => _selectedLanguage;
   set
      {   _selectedLanguage = value;
          AppSett.PrimaryLanguageOverride = value.LanguageCode;
Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride = value.LanguageCode;
          RaisePropertyChanged();
       }
}

1 个答案:

答案 0 :(得分:0)

这是一个已知问题,如果您想显示设置语言而无需重新启动,则可以在帧导航之前设置延迟。

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

有关更多信息,请参阅此案例reply