前段时候写了一个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. VS2010安装失败解决办法

    1. 运行regedit打开注册表: 2. 找到HKEY_LOCAL_MACHINE\SOFWARE\ Microsoft\ Internet Explorer\ MAIN: 3. MAIN子键的权限 ...

  2. WCF客户端承载

    http://www.cnblogs.com/wengyuli/archive/2010/12/27/1918109.html参考 Hi victory,   你提出的问题很好,这个问题,相信很多人学 ...

  3. IOS , plist 配置项说明

    本文转载至 http://blog.csdn.net/fengsh998/article/details/8307424 Key:Application can be killed immediate ...

  4. ios中将事件添加到系统日历

    - (void)saveEvent:(id)sender { //事件市场 EKEventStore *eventStore = [[EKEventStore alloc] init]; //6.0及 ...

  5. Codeforces 55D Beautiful Number (数位统计)

    把数位dp写成记忆化搜索的形式,方法很赞,代码量少了很多. 下面为转载内容:  a positive integer number is beautiful if and only if it is  ...

  6. AX 与Citrix打印机问题

    国外文章,抄个链接,备查 http://blogs.msdn.com/b/axsupport/archive/2010/07/06/ax-2009-citrix-amp-terminal-server ...

  7. Linux探秘之用户态与内核态

    一. Unix/Linux的体系架构 如上图所示,从宏观上来看,Linux操作系统的体系架构分为用户态和内核态(或者用户空间和内核).内核从本质上看是一种软件——控制计算机的硬件资源,并提供上层应用程 ...

  8. SkipList 跳表

    1.定义描述      跳跃列表(也称跳表)是一种随机化数据结构,基于并联的链表,其效率可比拟于二叉查找树(对于大多数操作需要O(log n)平均时间).      基本上,跳跃列表是对有序的链表增加 ...

  9. IOS的启动画面的适配问题

    iPhone4,iPhone4s 分辨率960*640 长宽比1.5iPhone5,iPhone5s 分辨率1136*640 长宽比1.775iPhone6 分辨率1334*750 长宽比1.778i ...

  10. TNS-12541: TNS: 无监听程序 TNS-12560: TNS: 协议适配器错误 TNS-00511: 无监听程序

    文章转自:http://www.luocs.com/archives/464.html 此文版权归作者 – yaogang所有,转载请注明yaogang©www.luocs.com. Luocs说:这 ...