c# - 在运行时更改 CurrentUICulture
问题描述
我有一个支持多种语言的 WPF 应用程序。
我在 Stackoverflow 上看到了对实现多语言(使用 .resx 文件)的最佳方式的问题的答案。一切都很好,只是语言在运行时不会改变。
我已经在 Stackoverflow 上看到了如何在运行时更改 CurrentUICulture问题的答案,但不幸的是,他的回答对我没有帮助,因为他在解决方案中发布的链接不起作用。
我在google上看到了很多文章,但没有成功。
我的一点尝试(.cs):
private void EventSetter_OnHandler(object sender, MouseButtonEventArgs e)
{
string item = ((ComboBoxItem)sender).Content.ToString();
switch (item)
{
case "English":
Console.WriteLine("English");
System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("en-GB");
System.Threading.Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("en-GB");
Product.Language.Resources.Culture = new System.Globalization.CultureInfo("en-GB");
break;
case "German":
Product.Language.Resources.Culture = new System.Globalization.CultureInfo("de-DE");
Console.WriteLine("German");
break;
default:
Console.WriteLine("none");
break;
}
}
XAML:
<Grid>
<StackPanel>
<Label x:Name="LabelHeader1" Content="{x:Static language1:Resources.LanguageTL}" />
<Label x:Name="LabelHeader2" Content="{x:Static language1:Resources.HomeTL}" />
<Label x:Name="LabelHeader3" Content="{x:Static language1:Resources.SkyTL}" />
<ComboBox x:Name="Languages">
<ComboBoxItem IsSelected="True">English</ComboBoxItem>
<ComboBoxItem>German</ComboBoxItem>
<ComboBox.ItemContainerStyle>
<Style>
<EventSetter Event="ComboBox.PreviewMouseDown" Handler="EventSetter_OnHandler" />
</Style>
</ComboBox.ItemContainerStyle>
</ComboBox>
</StackPanel>
</Grid>
密钥保存在.resx
文件中:
并感谢您的帮助
解决方案
推荐阅读
- android - 超过 26 岁的 Android 文件提供程序问题
- angular - 在扩展面板中突出显示扩展面板的单选按钮
- c# - 带有身份服务器 4 的 URL 令牌
- angular - (ngModelChange) 不会更新特定输入的 UI
- paypal - 对我的 Paypal 交易已通过 API 验证,但我的账户余额为 0.00 美元
- php - 我在处理 PHP 中的大 CSV 文件时遇到问题
- python - 如何将变量存储在外部文件中
- vb.net - 随机化列表中的字符串
- java - 处理清除缓存应用程序在 Asynctask 方法中出现错误
- extractor - 其用户无法更改安装路径的文件提取器