【问题标题】:WPF - How to change language property of a window dynamicallyWPF - 如何动态更改窗口的语言属性
【发布时间】:2015-11-11 19:43:38
【问题描述】:

我在应用程序中有一个用于购买产品的窗口。现在有两个选项 Local 或 Foreign如果用户选择外国,则应该是美元。

Window_Purchase.Language = ?

Window_Purchase 是我的窗口的名称。

如何在运行时更改语言属性。我不想只更改货币格式的文本语言。提前致谢。

【问题讨论】:

    标签: c# wpf localization globalization


    【解决方案1】:

    如果您有 2 个或更多资源文件,例如:
    (需要在Solution ExplorerProperties下添加)

    资源.resx

    资源.de.resx

    它们可以通过实现INotifyPropertyChanged下面的类来动态切换。

    namespace WpfApplication1.Properties
    {
        using System.Globalization;
        using System.ComponentModel;
        using System.Runtime.CompilerServices;
        using Properties;
    
        public class ResourceService : INotifyPropertyChanged
        {
            #region singleton members
    
            private static readonly ResourceService _current = new ResourceService();
            public static ResourceService Current
            {
                get { return _current; }
            }
            #endregion
    
            readonly Properties.Resources _resources = new Properties.Resources();
    
            public Properties.Resources Resources
            {
                get { return this._resources; }
            }
    
            #region INotifyPropertyChanged members
    
            public event PropertyChangedEventHandler PropertyChanged;
    
            protected virtual void RaisePropertyChanged([CallerMemberName] string propertyName = null)
            {
                var handler = this.PropertyChanged;
                if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
            }
    
            #endregion
    
            public void ChangeCulture(string name)
            {
                Resources.Culture = CultureInfo.GetCultureInfo(name);
                this.RaisePropertyChanged("Resources");
            }
        }
    }
    

    并且您要更改的文本(货币)必须绑定它以接收PropertyChanged 事件,如下所示:

    <!-- Add xmlns:properties-->
    <Window x:Class="WpfApplication1.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:properties="clr-namespace:WpfApplication1.Properties">
    
    <TextBlock Text="{Binding Source={x:Static properties:ResourceService.Current}, Path=Resources.Currency, Mode=OneWay}"
    

    然后,您可以动态更改Culture(Resources)。
    例如:

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        ResourceService.Current.ChangeCulture("de");
    }
    

    【讨论】:

    • 你是如何在资源文件的字符串值中添加€的?
    • @user5552042 刚刚复制粘贴。
    【解决方案2】:

    如果我猜对了,你不想更改申请中的文化信息吗?

    Application.CurrentCulture = System.Globalization.GetCultureInfo("en-us");
    

    https://msdn.microsoft.com/en-us/library/system.windows.forms.application.currentculture(v=vs.110).aspx

    【讨论】:

    • 不,我只希望它用于整个应用程序不需要的特定窗口
    • 我试过你给的代码。应用程序类没有任何 CurrentCulture 属性
    【解决方案3】:

    为当前表单尝试此方法

    System.Windows.FrameworkElement.LanguageProperty.OverrideMetadata(  
                    typeof( System.Windows.FrameworkElement ),  
                    new System.Windows.FrameworkPropertyMetadata(  
                        System.Windows.Markup.XmlLanguage.GetLanguage( System.Globalization.CultureInfo.CurrentCulture.IetfLanguageTag ) ) ); 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-05
      • 2011-05-28
      • 2019-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多