首页 > 解决方案 > 在运行时更改 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文件中:

.resx 文件截图

并感谢您的帮助

标签: c#wpf

解决方案


推荐阅读