我试图动态更改我的UWP应用的ui语言。 除了包含语言组合框本身的页面外,一切正常。
我必须从组合框中选择一种语言两次以查看页面更改为另一种语言,为什么我必须两次这样做?
private void comboUILanguages_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ResourceController.ChangeUILanguage((string)((ComboBox)sender).SelectedItem);
//for now, use a simple condition to determine wheter the page should be refreshed immediatly
if (true)
{
MainPage.Current.RefreshMenu();
Reload(currentGroup);
}
}
private bool Reload(object param = null)
{
var type = Frame.CurrentSourcePageType;
try
{
return Frame.Navigate(type, param);
}
finally
{
Frame.BackStack.Remove(Frame.BackStack.Last());
}
}
我的ResourceController:
class ResourceController
{
private static ResourceLoader resourceLoader = ResourceLoader.GetForViewIndependentUse();
public static string GetTranslation(string resource)
{
return resourceLoader.GetString(resource);
}
public static void ChangeUILanguage(string language)
{
Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride = language;
Windows.ApplicationModel.Resources.Core.ResourceContext.GetForViewIndependentUse().Reset();
}
public static string GetCurrentUILanguage()
{
return CultureInfo.CurrentUICulture.TwoLetterISOLanguageName;
}
}