WPF 多语言 多资源 多皮肤 处理方案
同时兼容这么多需求的解决方案 我想到的 只有通过 动态切换加载资源字典 前端用绑定的模式 达到托管最大化
多语言举例
我编辑了 两个 语言包 一个中文 一个英文 (语言包这个最好用T4 写个模板,这样添加新语言接口的时候不会遗漏,只是建议)
en-us.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib"> <s:String x:Key="Menu">Menu</s:String> <s:String x:Key="MenuClass">Class</s:String> <s:String x:Key="MenuGun">Gun</s:String> <s:String x:Key="LanguageType">en-us</s:String> </ResourceDictionary>
zh-cn.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib"> <s:String x:Key="Menu">菜单</s:String> <s:String x:Key="MenuClass">种类</s:String> <s:String x:Key="MenuGun">装备</s:String> <s:String x:Key="LanguageType">zh-cn</s:String> </ResourceDictionary>
这两个资源文件 我放到新建的文件夹 Languages 里
然后在 App.xaml 里手动绑定一个默认加载的语言方案 目的是为了 写代码的时候 绑定 可以使用 键值联想 再一个也可以直接在设计视图界面 直接查看绑定效果 方便修改和 调试
<Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="/Languages/en-us.xaml" /> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources>
添加个全局静态变量来维护记录当前使用的语言,为了方便配置启动的默认语言,我单独又在一个txt里 写了个值.我之所以没放到config里,是因为不想在给客户更新的时候把客户的个性配置方案个覆盖掉,打包发布程序必须得打包config,否则编译失败,目前我没找到更好的解决方案.
public class GlobalSettings { private static string _CurrentLanguage; public static string CurrentLanguage { get { if (string.IsNullOrEmpty(_CurrentLanguage)) { string languageFilePath = Environment.CurrentDirectory + "\\language.txt"; string strLanguage = string.Empty; string strDefaultLanguage = "zh-cn"; using (FileStream fs = new FileStream(languageFilePath, FileMode.OpenOrCreate,FileAccess.ReadWrite,FileShare.ReadWrite)) { using (StreamReader sr = new StreamReader(fs)) { strLanguage = sr.ReadToEnd().Trim().ToLower(); } } if (string.IsNullOrEmpty(strLanguage)) { using (FileStream fs = new FileStream(languageFilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite)) { strLanguage = strDefaultLanguage; using (StreamWriter sw = new StreamWriter(fs)) { sw.Write(strLanguage); } } } _CurrentLanguage = strLanguage; } return _CurrentLanguage; } set { _CurrentLanguage = value; } } }
程序入口窗体 随便放两个按钮 "中文" "英文" 用来切换系统的语言
<Grid> <Button Content="中文" Height="23" HorizontalAlignment="Left" Margin="78,139,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" /> <Button Content="英文" Height="23" HorizontalAlignment="Right" Margin="0,139,153,0" Name="button2" VerticalAlignment="Top" Width="75" Click="button2_Click" /> </Grid>
private void ShowWindow() { ]; if (merged!=null) { merged.Source = new Uri(string.Format("pack://application:,,,/Languages/{0}.xaml", GlobalSettings.CurrentLanguage)); App.Current.Resources.MergedDictionaries[] = merged; } Window1 w1 = new Window1(); w1.ShowDialog(); w1 = null; } private void button1_Click(object sender, RoutedEventArgs e) { GlobalSettings.CurrentLanguage = "zh-cn"; ShowWindow(); } private void button2_Click(object sender, RoutedEventArgs e) { GlobalSettings.CurrentLanguage = "en-us"; ShowWindow(); }
使用语言的绑定窗体 Window1.xaml
<Grid> <TextBlock Height="37" HorizontalAlignment="Left" Margin="31,33,0,0" Name="textBlock1" VerticalAlignment="Top" Width="83" Text="{DynamicResource Menu }" /> <TextBlock Height="37" HorizontalAlignment="Left" Margin="159,33,0,0" Name="textBlock2" Text="{DynamicResource MenuClass }" VerticalAlignment="Top" Width="83" /> <TextBlock Height="37" HorizontalAlignment="Left" Margin="31,104,0,0" Name="textBlock3" Text="{DynamicResource MenuGun }" VerticalAlignment="Top" Width="83" /> <TextBlock Height="37" HorizontalAlignment="Left" Margin="149,104,0,0" Name="textBlock4" Text="{DynamicResource LanguageType }" VerticalAlignment="Top" Width="83" /> </Grid>
这样只需要动态切换加载资源字典就好了
这样是直接编译到程序集里了 扩展性还差很多
可以把 一套资源字典编译到 一个单独的dll里 这种做法就比较类似 *.skin的做法 这种就是把动态加载资源 替换成 动态加载 dll
只是做了个简单的例子 提供个思路
WPF 多语言 多资源 多皮肤 处理方案的更多相关文章
- 项目笔记---WPF多语言方案
近期由于朋友邀请帮忙给一个开源的游戏“外挂”做一个I18N的解决方案,恰好也是WPF做的,之前有过相关经验,就忙了一个星期终于搞定了,已经提交给作者了,现在这里做一个分享. 这里分享下我个人Fork的 ...
- 一份关于Swift语言学习资源的整理文件
一份关于Swift语言学习资源的整理文件 周银辉 在这里下载 https://github.com/ipader/SwiftGuide
- (转)WPF控件开源资源
(转)WPF控件开源资源 Textbox Drag/Drop in WPFhttp://www.codeproject.com/Articles/42696/Textbox-Drag-Drop-in- ...
- Go语言学习资源
Go语言学习资源 下载:http://www.golangtc.com/downloadhttp://www.golangtc.com/download/liteide 安装及开发工具http://j ...
- WPF笔记(1.8 资源与映射)——Hello,WPF!
原文:WPF笔记(1.8 资源与映射)--Hello,WPF! 终于,看明白了,已经是凌晨1:39分.这本书这一节写得实在是不好,一个local搞得我糊里糊涂,于是,准备按照他的思路,按照我的理解,改 ...
- WPF中静态引用资源与动态引用资源的区别
WPF中静态引用资源与动态引用资源的区别 WPF中引用资源分为静态引用与动态引用,两者的区别在哪里呢?我们通过一个小的例子来理解. 点击“Update”按钮,第2个按钮的文字会变成“更上一层楼”, ...
- WPF 多语言解决方案 - Multilingual App Toolkit
1.首先安装Multilingual App Toolkit 2.新建项目,在VS中点击"工具" -> "Multilingual App Toolkit&qu ...
- WPF控件开源资源
(转)WPF控件开源资源 Textbox Drag/Drop in WPFhttp://www.codeproject.com/Articles/42696/Textbox-Drag-Drop-in- ...
- WPF中引入外部资源
有时候需要在WPF中引入外部资源,比如图片.音频.视频等,所以这个常见的技能还是需要GET到. 第一步:在VS中创建一个WPF窗口程序 第二步:从外部引入资源,这里以引入图片资源为例 1)新建Reso ...
随机推荐
- jquery获取表单的值
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- sc7731 Android 5.1 Camera 学习之一Camera 两个对象
众所周知,在Android中Camera采用了C/S架构,其中Camera server 与 Camera client之间通过Android Binder IPC机制进行通信.在Camera实现的框 ...
- Programming pages of Jasper Neumann
http://programming.sirrida.de/ Discussion topics Bit permutations Download source files List of func ...
- Codeforces Gym 100531I Instruction 构造
Problem I. Instruction 题目连接: http://codeforces.com/gym/100531/attachments Description Ingrid is a he ...
- [Angular2 Router] CanActivate Route Guard - An Example of An Asynchronous Route Guard
In this tutorial we are going to learn how we can to configure an can activate route guard in the An ...
- 关于ASP.NET中Button的OnClientClick属性
Button有Click属性和OnClientClick属性,执行顺序上OnClientClick先执行,调用本地脚本,根据返回值确定是否执行Click. 当返回True则执行Click,当脚本错误或 ...
- SQL Server 查看死锁的存储过程(转载)
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_who_lock]') and ) drop proc ...
- SVM多分类
http://www.matlabsky.com/thread-9471-1-1.htmlSVM算法最初是为二值分类问题设计的,当处理多类问题时,就需要构造合适的多类分类器.目前,构造SVM多类分类器 ...
- apache htaccess
#RewriteCond :重新条件#RewriteRule : 重写规则# .htaccess文件中的规则会以它们出现的顺序被处理 <IfModule mod_rewrite.c>Rew ...
- php & 引用
引用的作用: 如果程序比较大,引用同一个对象的变量比较多,并且希望用完该对象后手工清除它,个人建议用 "&" 方式,然后用$var=null的方式清除. 其它时候还是用ph ...