前段时候写了一个WPF多语言界面处理,个人感觉还行,分享给大家.使用合并字典,静态绑定,动态绑定.样式等东西

效果图

定义一个实体类LanguageModel,实际INotifyPropertyChanged接口

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel; namespace WpfApplication1
{
public class LanguageModel : INotifyPropertyChanged
{
private string _languageCode;
private string _languageName;
private string _languageDisplayName;
private string _resourcefile;
private bool _languageenabled;
/// <summary>
/// 语言代码 如en,zh
/// </summary>
public string LanguageCode
{
get { return _languageCode; }
set { _languageCode = value; OnPropertyChanged("LanguageCode"); }
}
/// <summary>
/// 语言名称 如:中国 ,English
/// </summary>
public string LanguageName
{
get { return _languageName; }
set { _languageName = value; OnPropertyChanged("LanguageName"); }
}
/// <summary>
/// 语言名称 如:中国 ,English
/// </summary>
public string LanguageDisplayName
{
get { return _languageDisplayName; }
set { _languageDisplayName = value; OnPropertyChanged("LanguageDisplayName"); }
} /// <summary>
/// 语言资源文件地址
/// </summary>
public string Resourcefile
{
get { return _resourcefile; }
set { _resourcefile = value; OnPropertyChanged("Resourcefile"); }
} /// <summary>
/// 是否启用此语言配置文件
/// </summary>
public bool LanguageEnabled
{
get { return _languageenabled; }
set { _languageenabled = value; OnPropertyChanged("LanguageEnabled"); }
} protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
} public event PropertyChangedEventHandler PropertyChanged;
}
}

核心类 I18N

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.IO; namespace WpfApplication1
{
public class Language : LanguageModel
{
private ResourceDictionary _resource; public ResourceDictionary Resource
{
get { return _resource; }
set { _resource = value; OnPropertyChanged("Resource"); }
}
} /// <summary>
/// 国际化 注:语言资源文件在VS2010的属性设置 复制到输出目录:始终复制 生成操作:内容
/// 资源文件 LanguageCode ,LanguageName, LanguageDisplayName,LanguageEnabled 字段必填
/// </summary>
public class I18N
{
private static string _currentLanguage = "zh-cn";
/// <summary>
/// 设置或获取语言编码,如果设置失败,则可能语言资源文件错误
/// </summary>
public static string CurrentLanguage
{
get { return _currentLanguage; }
set
{
if(UpdateCurrentLanguage(value))
_currentLanguage = value;
}
} private static List<Language> _languageIndex;
/// <summary>
/// 所有语言索引
/// </summary>
public static List<Language> LanguageIndex
{
get { return _languageIndex; }
} /// <summary>
/// 初始化,加载语言目录下的所有语言文件
/// </summary>
public static void Initialize()
{
_languageIndex = new List<Language>();
string dirstring = AppDomain.CurrentDomain.BaseDirectory + "Resource\\Language\\";
DirectoryInfo directory = new DirectoryInfo(dirstring);
FileInfo[] files = directory.GetFiles();
foreach (var item in files)
{
Language language = new Language();
ResourceDictionary rd = new ResourceDictionary();
rd.Source = new Uri(item.FullName);
language.LanguageCode = rd["LanguageCode"] == null ? "未知" : rd["LanguageCode"].ToString();
language.LanguageName = rd["LanguageName"] == null ? "未知" : rd["LanguageName"].ToString();
language.LanguageDisplayName = rd["LanguageDisplayName"] == null ? "未知" : rd["LanguageDisplayName"].ToString();
language.LanguageEnabled = rd["LanguageEnabled"] == null ? false : bool.Parse(rd["LanguageEnabled"].ToString());
language.Resourcefile = item.FullName;
language.Resource = rd;
if(language.LanguageEnabled)
_languageIndex.Add(language);
}
} /// <summary>
/// 更新语言配置. 同时同步CurrentLanguage字段
/// </summary>
private static bool UpdateCurrentLanguage(string LanguageCode)
{
if (LanguageIndex.Exists(P => P.LanguageCode == LanguageCode&&P.LanguageEnabled==true))
{
Language language = LanguageIndex.Find(P => P.LanguageCode == LanguageCode&&P.LanguageEnabled==true);
if (language != null)
{
foreach (var item in LanguageIndex)
{
Application.Current.Resources.MergedDictionaries.Remove(item.Resource);
}
Application.Current.Resources.MergedDictionaries.Add(language.Resource);
return true;
}
}
return false;
} /// <summary>
/// 查找语言资源文件具体的某项值,类似索引器
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static string GetLanguageValue(string key)
{
ResourceDictionary rd = Application.Current.Resources;
if (rd == null)
return "";
object obj = rd[key];
return obj == null ? "" : obj.ToString();
} }
}

资料文件直接使用 XX.xaml文件    注:语言资源文件在VS2010的属性设置   复制到输出目录:始终复制     生成操作:内容   ,确保xaml文件会复制到项目中,而不是编译到dll中

 <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib">
<sys:String x:Key="LanguageCode">zh</sys:String>
<sys:String x:Key="LanguageName">简休中文</sys:String>
<sys:String x:Key="LanguageDisplayName">简休中文</sys:String>
<sys:String x:Key="LanguageEnabled">true</sys:String> <sys:String x:Key="LanguageLanguage">语言:</sys:String>
<sys:String x:Key="LanguageA">字段A:</sys:String>
<sys:String x:Key="LanguageB">字段B:</sys:String>
<sys:String x:Key="LanguageC">字段C:</sys:String> </ResourceDictionary>

默认语言可以在App.xaml里设置

 <Application x:Class="WpfApplication1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Startup="Application_Startup"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resource/Language/zh.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>

界面上使用动态绑定或静态绑定资源  (DynamicResource  ,StaticResource) ,  DataGrid表头使用HeaderSytle

动态绑定:可以实时更新语言种类.

  静态绑定,不能实时更新语言种类,如:在登录的时候已经确实语言种类,进入系统后而不能更改.

示例效果界面

 <Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="" Width="">
<Window.Resources>
<Style x:Key="HeaderA" TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="Content" Value="{DynamicResource LanguageA}" />
</Style>
<Style x:Key="HeaderB" TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="Content" Value="{DynamicResource LanguageB}" />
</Style>
<Style x:Key="HeaderC" TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="Content" Value="{DynamicResource LanguageC}" />
</Style>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="40*" />
<RowDefinition Height="271*" />
</Grid.RowDefinitions>
<Label Content="{DynamicResource LanguageLanguage}" Height="" HorizontalAlignment="Left" Margin="26,12,0,0" Name="label1" VerticalAlignment="Top" />
<ComboBox Height="" HorizontalAlignment="Left" DisplayMemberPath="LanguageDisplayName" SelectedIndex="" Margin="160,12,0,0" Name="comboBox1" VerticalAlignment="Top" Width="" SelectionChanged="comboBox1_SelectionChanged" />
<DataGrid AutoGenerateColumns="False" Grid.Row="" Height="" HorizontalAlignment="Left" Margin="12,17,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="" >
<DataGrid.Columns>
<DataGridTemplateColumn Width="" HeaderStyle="{StaticResource HeaderA}" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox Name="chkStart" IsChecked="{Binding IsStart,UpdateSourceTrigger=PropertyChanged}" IsEnabled="{Binding Path=DataContext.IsEnabled, Mode=OneWay, RelativeSource={RelativeSource FindAncestor, AncestorType=DataGrid}}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Width="3*" HeaderStyle="{StaticResource HeaderB}" Binding="{Binding Path=Name, UpdateSourceTrigger=PropertyChanged}" IsReadOnly="True" />
<DataGridTextColumn Width="3*" HeaderStyle="{StaticResource HeaderC}" Binding="{Binding Path=Website, UpdateSourceTrigger=PropertyChanged,ValidatesOnDataErrors =True}" IsReadOnly="True" /> </DataGrid.Columns>
</DataGrid> </Grid>
</Window>

最后附上源代码  ,谢谢

End

技术在于分享,大家共同进步

WPF 实际国际化多语言界面的更多相关文章

  1. Blazor 国际化多语言界面 (I18nText )

    在实际使用中,我们经常会遇到需要把程序界面多种语言切换,适应不同地区使用者的需求,本文介绍一个我初学Blazor接触到的库,边撸边讲解. 包名: Toolbelt.Blazor.I18nText ht ...

  2. 为程序设置多语言界面——C#

    考虑到程序的国际化需求,需要为程序设置多语言界面. 1,新建一个资源文件,名字可以是对应界面+语言代码(MainForm.zh-CN).这样资源文件就会自动添加到对应界面下面. 2,更改界面属性Loc ...

  3. WPF 获得当前输入法语言区域

    原文:WPF 获得当前输入法语言区域 本文告诉大家如何获得 WPF 输入法的语言区域 需要使用 user32 的方法,很简单,请看下面 [DllImport("user32.dll" ...

  4. iOS 国际化多语言设置 xcode7

    iOS 国际化多语言设置 方式一: 1. 在storyboard中创建好UI,然后在 project 里面  Localizables 栏目里面,添加你需要的语言:默认是Englist; 比如这里我添 ...

  5. WPF如何实现类似iPhone界面切换的效果(转载)

    WPF如何实现类似iPhone界面切换的效果 (version .1) 转自:http://blog.csdn.net/fallincloud/article/details/6968764 在论坛上 ...

  6. WPF换肤之四:界面设计和代码设计分离

    原文:WPF换肤之四:界面设计和代码设计分离 说起WPF来,除了总所周知的图形处理核心的变化外,和Winform比起来,还有一个巨大的变革,那就是真正意义上做到了界面设计和代码设计的分离.这样可以让美 ...

  7. WPF下的视频录制界面设计

    原文:WPF下的视频录制界面设计 在去年12月份,我曾经写过三篇文章讨论C#下视频录制.播放界面的设计.这三篇文章是:利用C#画视频录制及播放的界面(一) 利用C#画视频录制及播放的界面(二)利用C# ...

  8. 更好用的excel国际化多语言导出

    不知道大家在开发中有没有遇到过『excel导出』的需求,反正我最近写了不少这种功能,刚开始利用poi,一行行的手动塞数据,生成excel,而且还有国际化需求,比如:标题栏有一列,用户切换成" ...

  9. [Spring]Spring Mvc实现国际化/多语言

    1.添加多语言文件*.properties F64_en_EN.properties详情如下: F60_G00_M100=Please select data. F60_G00_M101=Are yo ...

随机推荐

  1. android 检测网络是否可用

    /**     * 检测网络是否可用     *      * @return     */    public boolean isNetworkConnected() {        Conne ...

  2. Servlet3.0学习总结——基于Servlet3.0的文件上传

    Servlet3.0学习总结(三)——基于Servlet3.0的文件上传 在Servlet2.5中,我们要实现文件上传功能时,一般都需要借助第三方开源组件,例如Apache的commons-fileu ...

  3. C++ STL轻松导学

    作为C++标准不可缺少的一部分,STL应该是渗透在C++程序的角角落落里的.STL不是实验室里的宠儿,也不是程序员桌上的摆设,她的激动人心并非昙花一现.本教程旨在传播和普及STL的基础知识,若能借此机 ...

  4. Rhino -- 基于java的javascript实现

    http://www.cnblogs.com/cczw/archive/2012/07/16/2593957.html

  5. 开发者app 上传收集

    直接使用酷传  看各个市场目录 http://publish.coolchuan.com/myaccount/accounts 注册帐号 百度开发者 http://jingyan.baidu.com/ ...

  6. strcpy和memcpy的区别

    strcpy和memcpy都是标准C库函数,它们有下面的特点.strcpy提供了字符串的复制.即strcpy只用于字符串复制,并且它不仅复制字符串内容之外,还会复制字符串的结束符. 已知strcpy函 ...

  7. 使用 crosswalk-cordova 打包sencha touch 项目,再也不用担心安卓兼容问题!

    国内的安卓手机品牌众多,安卓操作系统碎片化也很严重,我们使用sencha touch 开发的应用不可避免的出现了各种无解的兼容性问题. 有时候我就在想,有没有既能支持cordova,又能让我们把Chr ...

  8. 非常简单实用的Python HTTP服务

    在做分布式系统应用的时候经常在测试环境上传一个包,或者干嘛的,公司的服务器比较bug,只给ldap权限,每次只能scp到自己的个人目录下,然后才能进到公共账号下去cp,比较麻烦.这时候如果你需要一个简 ...

  9. python 字符串相加

    我们通过操作符号+来进行字符串的相加,不过建议还是用其他的方式来进行字符串的拼接,这样效率高点. 原因:在循环连接字符串的时候,他每次连接一次,就要重新开辟空间,然后把字符串连接起来,再放入新的空间, ...

  10. Java知多少(112)数据库之删除记录

    删除数据表也有3种方案 一.使用Statement对象 删除数据表记录的SQL语句的语法是: delete from 表名 where 特定条件 例如 : delete from ksInfo whe ...