我无法更改应用的语言。当用户更改组合框中的所选项目时,应更改语言。
以下是我的部分代码的片段:
protected override void OnLaunched(LaunchActivatedEventArgs args)
{
ResourceContext.GetForCurrentView().QualifierValues.MapChanged += QualifierValues_MapChanged;
}
private void QualifierValues_MapChanged(IObservableMap<string, string> sender, IMapChangedEventArgs<string> @event)
{
ResourceContext.ResetGlobalQualifierValues();
}
组合框代码是:
private void cbLanguage_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.AddedItems.Count > 0 && !String.IsNullOrEmpty(((ComboBoxItem)e.AddedItems[0]).Tag as string))
{
LocalizationManager.UICulture = new System.Globalization.CultureInfo((string)((ComboBoxItem)e.AddedItems[0]).Tag);
Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride = LocalizationManager.UICulture.Name;
int index = 0;
if (cbLanguage.SelectedIndex == 0 || cbLanguage.SelectedIndex < 0)
{
index = cbLanguage.Items.Count - 1;
}
else
{
index = cbLanguage.SelectedIndex - 1;
}
Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride = (string)((ComboBoxItem)cbLanguage.Items[index]).Tag as String;
Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride = ((ComboBoxItem)e.AddedItems[0]).Tag as String;
if (languageChange != null)
{
languageChange(sender, new EventArgs());
}
}
else
{
LocalizationManager.UICulture = System.Globalization.CultureInfo.CurrentUICulture;
}
}
这会返回访问冲突错误。
你可以帮助我,让应用程序正确更改语言吗?
答案 0 :(得分:0)
代码正在设置Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride
三次。这是什么原因?
设置primay语言后,为什么要更改索引并再次设置语言?
if (cbLanguage.SelectedIndex == 0 || cbLanguage.SelectedIndex < 0)
{
index = cbLanguage.Items.Count - 1;
}
else
{
index = cbLanguage.SelectedIndex - 1;
}