簡體   English   中英

如何在更改文化/語言后刷新WPF窗口和控件

[英]How do i refresh WPF window and controls after changing the culture/language

我在VS 2015上使用WPF測試應用程序來處理一個新項目,用戶可以在運行時更改語言。

所以我根據這篇文章制作了我的測試項目。

我在Proerties文件夾中添加了三個RESX文件。

在此輸入圖像描述

然后我添加了一個構造函數和方法來切換語言。

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刷新窗口但沒有任何反應。

大問題:如何在運行時刷新窗口?

PS:通過使用XAML而不是RESX,這沒有問題(除了驗證的錯誤消息)。

感謝Felix的暗示,我實施了Tomer Shamam的解決方案。 此解決方案可以在運行時更改語言。 此外,所選語言用於WPF項目的所有打開窗口。 您可以在GitHub上查看我的testproject。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM