前段时候写了一个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. asp.net发布到IIS中出现错误:处理程序“PageHandlerFactory-Integrated”在其模块列表中有一个错误模块“ManagedPipelineHandler”

    asp.net发布到IIS中出现错误:处理程序“PageHandlerFactory-Integrated”在其模块列表中有一个错误模块“ManagedPipelineHandler” http:// ...

  2. Navi.Soft30.开放平台.聚合.开发手册

    1系统简介 1.1功能简述 现在是一个信息时代,并且正在高速发展.以前获取信息的途径非常少,可能只有电视台,收音机等有限的来源,而现在的途径数不胜数,如:QQ,微信,官方网站,个人网站等等 本开发手册 ...

  3. AX2012 R3升级CU8的一些错误

    AX2012 R3安装升级包CU8后进入系统,系统会提示打开软件升级清单“Software update checklist”,清单列出了升级要做的一系列动作. 在进行到编译应用时“Compile a ...

  4. 使用before、after伪类制作三角形

    使用before.after伪类实现三角形的制作,不需要再为三角形增加不必要的DOM元素,影响阅读. <!DOCTYPE html><html><head>    ...

  5. 自己开发的csdn手机客户端

    本人开发的,同步csdn官网新闻和博客内容,支持本地浏览,而且还可以手机上看到博客中的代码! 这是一款同步更新官网最新的资讯信息应用软件. 全新的用户界面,更好的用户体验,数据加载速度得到了进一步优化 ...

  6. C# 使用 SAP NCO3.0 调用SAP RFC函数接口

    最近使用C#调用SAP RFC函数,SAP提供了NCO3.0组件. 下载组件安装,之后引用“sapnco.dll”和“sapnco_utils.dll”两个文件. 在程序中 using SAP.Mid ...

  7. java之内部类(InnerClass)----非静态内部类、静态内部类、局部内部类、匿名内部类

    提起java内裤类(innerClass)很多人不太熟悉,实际上类似的概念在c++里面也有,那就是嵌套类(Nested Class),关于这俩者的区别,在下文中会有对比.内部类从表面上看,就是在类中定 ...

  8. Android ListView OnItemLongClick和OnItemClick事件内部细节分享以及几个比较特别的属性

    本文转自 http://blog.sina.com.cn/s/blog_783ede030101bnm4.html 作者kiven 辞职3,4个月在家休息,本以为楼主要程序员逆袭,结果失败告终继续码农 ...

  9. wamp2.5 不能运行在win2003的解决方法

    安装时提示 httpd.exe 不是有效的 win32程序 之后就启动不了,连小icon都不显示了 经查发现 wampserver 2.5用 vc11编译,并使用了他的类库 vc11是不支持 xp和 ...

  10. maven eclipse jetty debug

    可以通过查看最近版本: http://mvnrepository.com/artifact/org.eclipse.jetty/jetty-server http://search.maven.org ...