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 ...
随机推荐
- 获取oracle 表字段,表名,以及主键之类等等的信息
数据库版本号:select * from v$version 数据库名:select * from v$instance 注意: 我在C#项目中查询语句的时候报“ORA-00911: 无效字符” 的错 ...
- 企业应用架构模式阅读笔记 - Martin Fowler
1. 数据读取
- 安装nodejs和grunt以后出现 /usr/bin/env: node: No such file or directory
安装完成以后需要执行此命令 sudo ln -s /usr/bin/nodejs /usr/bin/node
- Codeforce Gym 100015I Identity Checker 暴力
Identity Checker 题目连接: http://codeforces.com/gym/100015/attachments Description You likely have seen ...
- Codeforces Gym 100463E Spies 并查集
Spies Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100463/attachments Desc ...
- andorid 中如何实现双击事件
项目需求: android中只有单击和其他事件,其实都是由OnTouch事件演变而来:最近有项目要求双击全屏,所以就试着实现了下 具体实现如下: 1.MainActivity.java实现: publ ...
- DataInputStream和DataOutputStream使用方法细节探讨
DataInputStream和DataOutputStream都是Java中输入输出流的装饰类,用起来非常方便.今天就来讨论一下使用该类时候遇到的编码问题. package com.vince ...
- Linux下高并发socket链接数测试
一.如何增大service进程的max open files ulimit -n 只能改小max open files,不能改大.需要按照以下步骤: 修改/etc/security/limits.co ...
- 【百度地图-安卓SDK】从头开始写android程序
[百度地图-安卓SDK]从头开始写android程序首先确保有这四个文件 安装jdk先安装android开发SDK(并不只是为eclipse服务的),即运行installer_r15-windo ...
- iOS开发——动画编程Swift篇&(三)CATransition动画
CATransition动画 // MARK: - CATransition动画 // /* 动画样式 */ // let kCATransitionFade: NSString! //翻页 // l ...