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 ...
随机推荐
- sql 将一个表中的数据插入到另一个表中
列名不一定要相同,只要你在HH中列出要插入列的列表跟select from mm表中的选择的列的列表一一对应就可以了,当然两边的数据类型应该是兼容的. 比如:insert into h ...
- javascript、jquery获取网页的高度和宽度
javascript: 可视区域宽 :document.documentElement.clientWidth (width + padding) 可视区域高 :document.documentE ...
- 使用 preferredStatusBarStyle 设置状态栏颜色
iOS9之前,在plist文件中 插入一个新的key,名字为View controller-based status bar appearance,并将其值设置为NO. 然后敲入代码: [UIAppl ...
- YOUYOU深入学习Ganglia之三(gmetad的软件架构)
Ganglia这个东西,目前的情况是测试的多,真正在数据中心部署过的人少:使用的多,真正能了解其代码架构的人少.这里根据我的经验,分解一下ganglia的gmetad的软件架构,欢迎大家交流. 上面的 ...
- 12个非常不错的免费HTML后台管理模板
下面介绍的这些免费后端管理HTML模板,都非常不错.建议您收藏. 1. Charisma Admin Template (示例) Charisma是一个响应式管理模板,基于Twitter Boots ...
- js设置控件的隐藏与显示的两种方法
js设置控件的隐藏与显示,设置控件style的display和visibility属性就可以了,下面有个示例,需要的朋友可以参考下用JavaScript隐藏控件的方法有两种,分别是通过设置控件的sty ...
- 50个Android开发人员必备UI效果源码[转载]
50个Android开发人员必备UI效果源码[转载] http://blog.csdn.net/qq1059458376/article/details/8145497 Android 仿微信之主页面 ...
- angularJs项目实战!02:前端的页面分解与组装
自从上一篇文章到现在已经有将近一个月的时间,我将精力放在了前端页面分解与组装,和angularjs如何与jquery.bootstrap.D3等一系列其他类库结合使用的经验总结上.由于公司新招了一些员 ...
- oc-15-self
// // Person.m // OC基础第三天 // // Created by apple on 15/10/17. // // #import "Person.h" @im ...
- Flex4+Spring3+Hibernate3+BlazeDS整合笔记
普通Java Web工程流行使用ssh框架,而当前台使用Flex制作的时候,后台就不需要用Struts了,通过使用BlazeDS远程方法调用即可. 首先,新建Java Web工程,然后添加Flex项目 ...