微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 如何在更改文化/语言后刷新WPF窗口和控件

我在VS 2015上使用 WPF测试应用程序来处理一个新项目,用户可以在运行时更改语言.

所以我根据这个article制作了我的测试项目.

我在Proerties文件夹中添加了三个RESX文件.

enter image description here

然后我添加一个构造函数方法来切换语言.

namespace MultipleLanguages
{
    /// <summary>
    /// Interaktionslogik für "App.xaml"
    /// </summary>
    public partial class App : Application
    {
        /// <summary>
        /// The constructor.
        /// </summary>
        public App()
        {
            // Sets the desired language.
            ChangeLanguage("de-DE");
        }

        /// <summary>
        /// Switches to language german.
        /// </summary>
        public void SwitchToLanguageGerman()
        {
            ChangeLanguage("de-DE");
        }

        /// <summary>
        /// Switches to language english.
        /// </summary>
        public void SwitchToLanguageEnglish()
        {
            ChangeLanguage("en-US");
        }

        /// <summary>
        /// Changes the language according to the given culture.
        /// </summary>
        /// <param name="culture">The culture.</param>
        private void ChangeLanguage(string culture)
        {
            System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture);
            System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo(culture);
            MultipleLanguages.Properties.Resources.Culture = new CultureInfo(culture);
        }

    }
}

最后,我将资源实现到我的WPF窗口.

首先是XAML:

<Window x:Class="MultipleLanguages.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:MultipleLanguages"
        xmlns:mlres="clr-namespace:MultipleLanguages.Properties"
        mc:Ignorable="d"
        Title="{x:Static mlres:Resources.mainwindowtitle}"
        Height="350"
        Width="525">

    <Grid x:Name="Grd_mainpanel">
        <Grid.RowDeFinitions>
            <RowDeFinition/>
            <RowDeFinition Height="Auto"/>
        </Grid.RowDeFinitions>
        <Grid.ColumnDeFinitions>
            <ColumnDeFinition/>
        </Grid.ColumnDeFinitions>

        <Grid x:Name="Grd_persondatapanel"
              Grid.Row="0"
              Grid.Column="0">
            <Grid.RowDeFinitions>
                <RowDeFinition Height="Auto"/>
                <RowDeFinition Height="Auto"/>
            </Grid.RowDeFinitions>
            <Grid.ColumnDeFinitions>
                <ColumnDeFinition/>
                <ColumnDeFinition/>
            </Grid.ColumnDeFinitions>

            <Label Grid.Row="0"
                   Grid.Column="0"
                   Content="{x:Static mlres:Resources.firstname}"
                   Margin="5"></Label>

            <Label Grid.Row="1"
                   Grid.Column="0"
                   Content="{x:Static mlres:Resources.lastname}"
                   Margin="5"></Label>
        </Grid>

        <WrapPanel x:Name="wrp_buttonpanel"
                   Grid.Row="1"
                   Grid.Column="0">
            <!--Test to get values from the resources-->
            <Button x:Name="btn_testresource"
                    Margin="5"
                    Click="btn_testresource_Click">Test</Button>
            <!--Switch to language german-->
            <Button x:Name="btn_switchtogerman"
                    Margin="5"
                    Click="btn_switchtogerman_Click"
                    Content="{x:Static mlres:Resources.switchtogerman}"></Button>
            <!--Switch to language english-->
            <Button x:Name="btn_switchtoenglish"
                    Margin="5"
                    Click="btn_switchtoenglish_Click"
                    Content="{x:Static mlres:Resources.switchtoenglish}"></Button>
        </WrapPanel>
    </Grid>

</Window>

而C#代码

public partial class MainWindow : Window
{
    /// <summary>
    /// The constructor.
    /// </summary>
    public MainWindow()
    {
        InitializeComponent();
    }

    /// <summary>
    /// For testing the resource dictionaries.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void btn_testresource_Click(object sender,RoutedEventArgs e)
    {
        // Shows the name of the current culture.
        System.Globalization.CultureInfo ci = System.Globalization.CultureInfo.CurrentCulture;
        MessageBox.Show("The name of the current culture is '" + ci.Name + "'");

        ////// Shows the text to the resource key "firstname" according to the current culture.
        ////ResourceManager rm1 = new ResourceManager("MultipleLanguages.TextResource1",System.Reflection.Assembly.GetExecutingAssembly());
        ////MessageBox.Show("'firstname' aus MultipleLanguages.TextResource1" + rm1.GetString("firstname"));
        ////ResourceManager rm2 = new ResourceManager("MultipleLanguages.TextResources.TextResource2",System.Reflection.Assembly.GetExecutingAssembly());
        ////MessageBox.Show("'firstname' aus MultipleLanguages.TextResources.TextResource2" + rm2.GetString("firstname"));

        // Shows values to the given names from the resource - according to the current culture,which was set in App.xaml.cs.
        ResourceManager rm = MultipleLanguages.Properties.Resources.ResourceManager;
        MessageBox.Show("firstname : " + rm.GetString("firstname"));
        MessageBox.Show("lastname : " + rm.GetString("lastname"));
    }

    private void btn_switchtogerman_Click(object sender,RoutedEventArgs e)
    {
        ((App)Application.Current).SwitchToLanguageGerman();
        UpdateLayout();
    }

    private void btn_switchtoenglish_Click(object sender,RoutedEventArgs e)
    {
        ((App)Application.Current).SwitchToLanguageEnglish();
        UpdateLayout();
    }

}

启动后,认语言为德语.

当我切换到英语时,线程的当前文化被改变,但我没有看到GUI的任何变化.

但当我点击“测试”按钮时,我从资源中获取英文值.

最后我尝试使用UpdateLayout刷新窗口但没有任何反应.

大问题:如何在运行时刷新窗口?

P.S.:通过使用XAML而不是RESX,这没有问题(除了来自验证的错误消息).

解决方法

感谢Felix的暗示,我实施了 Tomer Shamam解决方案.
解决方案可以在运行时更改语言.
此外,所选语言用于WPF项目的所有打开窗口.
您可以在 GitHub上查看我的测试项目.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐