关闭

WPF 国际化具体实现步骤

标签: 国际化wpf数据绑定
673人阅读 评论(0) 收藏 举报
分类:

这是本人第一次写博客,心想在园子里已经“潜伏”两年多了,好像是该做点什么了,从此下定决心开始写博客。

虽然说文章标题写着原创,但是其实还是借鉴了一些文章,我只是把自己的想法融合进去

如有版权问题,请及时联系我....

废话不多说,切入正题...

 

想法:

1.把需要国际化的数据保存在相应的wpf的资源文件中。

2.创建数据实体类(用来切换国际化数据)

3.把实体类的数据绑定到UI界面上。

 

具体实践:

1.创建两个资源文件Chinese.xamlEnglish.xaml

Chinese.xaml:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:sys="clr-namespace:System;assembly=mscorlib"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <sys:String x:Key="MainTitleName">数据转发</sys:String>
</ResourceDictionary>


 

English.xaml:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:sys="clr-namespace:System;assembly=mscorlib"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <sys:String x:Key="MainTitleName">Data Forwarder</sys:String>
    
</ResourceDictionary>



 

2.创建数据实体类

public class InternationalizationNames:INotifyPropertyChanged
    {
        public string MainTitleName
        {
            get
            {
                object obj = Application.Current.FindResource("MainTitleName");
                string str = obj as string;
                return str; 
            }
        }
 
        public void UpdateInternationalizationNames()
        {
            this.OnPropertyChanged("MainTitleName");
        }
 
        private void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler
                = this.PropertyChanged;
 
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
 
        public event PropertyChangedEventHandler PropertyChanged;
    }

 

3.自定义绑定类

public class ResourceManager
    {
        public static InternationalizationNames AllInternationalNames;
        public static ResourceDictionary LanguageResourceDictionary;
 
        static ResourceManager()
        {
            AllInternationalNames = new InternationalizationNames();
            LanguageResourceDictionary = new ResourceDictionary();
            
        }
 
        public ResourceManager()
        {
     
            
        }
 
        
    }

public class AllInternationalNamesBinding : System.Windows.Data.Binding
    {
        public AllInternationalNamesBinding()
        {
            this.Source = ResourceManager.AllInternationalNames;
        }
        public AllInternationalNamesBinding(string path)
        {
            this.Path = new PropertyPath(path);
            this.Source = ResourceManager.AllInternationalNames;
        }
    }



 

4.数据绑定UI

<Window x:Class="DDEDataAchieve.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:tools="clr-namespace:DDEDataAchieve.Tools"
        mc:Ignorable="d"
        Height="600"
        Width="800"
        WindowStartupLocation="CenterScreen"
        Title="{tools:AllInternationalNamesBinding Path=MainTitleName}"
        TextOptions.TextFormattingMode="Display">


注意:DDEDataAchieve.Tools为类AllInternationalNamesBinding的命名空间。

 

5.国际化语言切换

public partial class App : Application
    {
        static App()
        {
            DispatcherHelper.Initialize();
        }
 
        ResourceManager resourceManager;
        public App()
        {
            resourceManager = new ResourceManager();
            this.Startup += new StartupEventHandler(App_Startup);
        }
 
        void App_Startup(object sender, StartupEventArgs e)
        {
            ResourceManager.LanguageResourceDictionary.Source = 
                new Uri("Resources/English.xaml", UriKind.Relative);
 
            Application.Current.Resources.MergedDictionaries.Add(ResourceManager.LanguageResourceDictionary);
        }


按钮切换语言事件中如下:

Application.Current.Resources.MergedDictionaries.Remove(ResourceManager.LanguageResourceDictionary);
 
        ResourceManager.LanguageResourceDictionary.Source =
                new Uri("Resources/Chinese.xaml", UriKind.Relative);
 
        Application.Current.Resources.MergedDictionaries.Add(ResourceManager.LanguageResourceDictionary);
 
        ResourceManager.AllInternationalNames.UpdateInternationalizationNames();


这样就实现了WPF国际化的问题。

本文如有任何问题欢迎随时指正。

 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:16877次
    • 积分:338
    • 等级:
    • 排名:千里之外
    • 原创:17篇
    • 转载:0篇
    • 译文:0篇
    • 评论:1条
    文章分类