WPF 语言包自定义与使用

WPF 语言包

1定义语言资源

Luanguage文件夹下添加StringResource.en-US.xamlStringResource.zh-CN.xaml分别定义语言为英语和中文的语言资源文件。

StringResource.en-US.xaml


    File
    Help
    About


StringResource.zh-CN.xaml


    
    文件
    帮助
    关于


2引入资源

在App.xaml文件中将资源文件添加到App的ResourceDictionaryMergedDictionaries资源字典集合中。


    
        
            
                
                
            
        
    


3使用语言资源

1.在xaml文件中使用:

可以在任何View视图使用语言资源,但是注意:一定要使用DynamicResource进行定义。


    

        
            
            
                
            
        
        
    


2.在cs文件中使用:

可以通过查找资源来使用资源:

(string)Application.Current.Resources["biHelp"]

4语言切换

4.1在App中添加Language静态属性,当属性发生变化时,更新语言包,程序默认情况下,设置一个语言"en-US"

    public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);

            // 初始化语言
            Language = string.IsNullOrEmpty(Language) ? "en-US" : Language;
        }

        private static string language;

        public static string Language
        {
            get { return language; }
            set
            {
                if (language != value)
                {
                    language = value;
                    List dictionaryList = new List();
                    foreach (ResourceDictionary dictionary in Application.Current.Resources.MergedDictionaries)
                    {
                        dictionaryList.Add(dictionary);
                    }
                    string requestedLanguage = string.Format(@"Language\StringResource.{0}.xaml", Language);
                    ResourceDictionary resourceDictionary = dictionaryList.FirstOrDefault(d => d.Source.OriginalString.Equals(requestedLanguage));
                    if (resourceDictionary == null)
                    {
                        requestedLanguage = @"Language\StringResource.en-US.xaml";
                        resourceDictionary = dictionaryList.FirstOrDefault(d => d.Source.OriginalString.Equals(requestedLanguage));
                    }
                    if (resourceDictionary != null)
                    {
                        Application.Current.Resources.MergedDictionaries.Remove(resourceDictionary);
                        Application.Current.Resources.MergedDictionaries.Add(resourceDictionary);
                    }
                }
            }
        }
    }

4.2添加绑定命令

4.2.1绑定命令定义

 	public class Commands
    {
        /// 
        /// 语言选择命令
        /// 
        private static RoutedUICommand chooseLanguage = new RoutedUICommand("ChooseLanguage", "ChooseLanguage", typeof(Commands));
        public static RoutedUICommand ChooseLanguage
        {
            get { return chooseLanguage; }
        }
    }

4.2.2命令绑定


    
        
    

    

        
            
            
                
                    
                    
                
            
            
                
            
        
        
    


4.2.3命令处理

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void ChooseLanguage_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
            e.Handled = true;
        }

        private void ChooseLanguage_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            //英文
            if (e.Parameter.ToString() == "en")
            {
                App.Language = "en-US";
            }
            //中文
            else if (e.Parameter.ToString() == "zh")
            {
                App.Language = "zh-CN";
            }
        }
    }

5效果

WPF 语言包自定义与使用_第1张图片

WPF 语言包自定义与使用_第2张图片


积跬步以至千里:) (:一阵没来由的风

你可能感兴趣的:(C#,WPF)