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 ...
随机推荐
- c语言-格式控制字符 %XXd 用法
d格式字符 用来输出十进制整数,有以下几种用法: 1. %d, 按整型数据的实际长度输出. 2. %md,m为指定输出的整型位数的宽度,如果整型数据的实际位数小于m,则左端补以空格,如果大于m,则按 ...
- sc7731 Android 5.1 Camera 学习之一Camera 两个对象
众所周知,在Android中Camera采用了C/S架构,其中Camera server 与 Camera client之间通过Android Binder IPC机制进行通信.在Camera实现的框 ...
- 直接运行PowerShell脚本
以管理员权限运行下面语句:ftype Microsoft.PowerShellScript.1="C:\WINDOWS\system32\windowspowershell\v1.0\pow ...
- Codeforces Gym 100803D Space Golf 物理题
Space Golf 题目连接: http://codeforces.com/gym/100803/attachments Description You surely have never hear ...
- C#操作Excel(1)Excel对象模型
Excel对象模型 (.Net Perspective) 本文主要针对在Visual Studio中使用C# 开发关于Excel的应用程序 本文的PDF下载地址:C#操作Excel2007.pdf ...
- Some tips on using HashSet<T> and List<T>
This article is written based on my colleague's review Most of the times, when I want to use a colle ...
- UVA11038- How Many O's?(组合数学)
题目链接 题意:求出在a到b之间的数中,有多少个0. 思路:组合数学问题.能够枚举每一个位置上的数i,如果i之前的数为left,后面的为right,后面有num位数.当i != 0时,将i置为0,所以 ...
- 终端I/O之终端选项标志
http://www.cnblogs.com/nufangrensheng/p/3575752.html 中的表18-1至表18-4中列出的所有选项标志(除屏蔽标志外)都用一位或几位(设置或清除)表示 ...
- win7配置nginx+php步骤
1.下载nginx: http://www.nginx.cn/nginx-download 2.下载php : http://www.php.net/downloads.php (线程安全与非安全参 ...
- linux用户权限
Linux下passwd和shadow文件内容详解 一./etc/passwd/etc/passwd 文件是一个纯文本文件,每行采用了相同的格式: name:password:uid:gid:comm ...