同时兼容这么多需求的解决方案 我想到的 只有通过 动态切换加载资源字典 前端用绑定的模式 达到托管最大化
多语言举例
我编辑了 两个 语言包 一个中文 一个英文 (语言包这个最好用T4 写个模板,这样添加新语言接口的时候不会遗漏,只是建议)
en-us.xaml
class="code_img_closed" src="/Upload/Images/2013102917/0015B68B3C38AA5B.gif" alt="" />logs_code_hide('2eca4511-6bad-4886-859f-e7c7fc1ef42c',event)" src="/Upload/Images/2013102917/2B1B950FA3DF188F.gif" alt="" /><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>View Code
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>View Code
这两个资源文件 我放到新建的文件夹 Languages 里
然后在 App.xaml 里手动绑定一个默认加载的语言方案 目的是为了 写代码的时候 绑定 可以使用 键值联想 再一个也可以直接在设计视图界面 直接查看绑定效果 方便修改和 调试
<Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="/Languages/en-us.xaml" /> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources>View Code
添加个全局静态变量来维护记录当前使用的语言,为了方便配置启动的默认语言,我单独又在一个txt里 写了个值.我之所以没放到config里,是因为不想在给客户更新的时候把客户的个性配置方案个覆盖掉,打包发布程序必须得打包config,否则编译失败,目前我没找到更好的解决方案.
1 public class GlobalSettings 2 { 3 4 private static string _CurrentLanguage; 5 6 public static string CurrentLanguage 7 { 8 get 9 { 10 if (string.IsNullOrEmpty(_CurrentLanguage)) 11 { 12 string languageFilePath = Environment.CurrentDirectory + "\\language.txt"; 13 14 string strLanguage = string.Empty; 15 string strDefaultLanguage = "zh-cn"; 16 17 using (FileStream fs = new FileStream(languageFilePath, FileMode.OpenOrCreate,FileAccess.ReadWrite,FileShare.ReadWrite)) 18 { 19 using (StreamReader sr = new StreamReader(fs)) 20 { 21 strLanguage = sr.ReadToEnd().Trim().ToLower(); 22 } 23 } 24 25 if (string.IsNullOrEmpty(strLanguage)) 26 { 27 using (FileStream fs = new FileStream(languageFilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite)) 28 { 29 strLanguage = strDefaultLanguage; 30 using (StreamWriter sw = new StreamWriter(fs)) 31 { 32 sw.Write(strLanguage); 33 } 34 } 35 } 36 37 _CurrentLanguage = strLanguage; 38 39 } 40 41 return _CurrentLanguage; 42 43 } 44 set { _CurrentLanguage = value; } 45 } 46 47 48 }View Code
程序入口窗体 随便放两个按钮 "中文" "英文" 用来切换系统的语言
<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>View Code
1 private void ShowWindow() 2 { 3 4 5 var merged = App.Current.Resources.MergedDictionaries[0]; 6 if (merged!=null) 7 { 8 merged.Source = new Uri(string.Format("pack://application:,,,/Languages/{0}.xaml", GlobalSettings.CurrentLanguage)); 9 App.Current.Resources.MergedDictionaries[0] = merged; 10 } 11 12 Window1 w1 = new Window1(); 13 w1.ShowDialog(); 14 w1 = null; 15 } 16 17 18 private void button1_Click(object sender, RoutedEventArgs e) 19 { 20 GlobalSettings.CurrentLanguage = "zh-cn"; 21 ShowWindow(); 22 } 23 24 private void button2_Click(object sender, RoutedEventArgs e) 25 { 26 GlobalSettings.CurrentLanguage = "en-us"; 27 ShowWindow(); 28 }View Code
使用语言的绑定窗体 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>View Code
这样只需要动态切换加载资源字典就好了
这样是直接编译到程序集里了 扩展性还差很多
可以把 一套资源字典编译到 一个单独的dll里 这种做法就比较累死 *.skin的做法 这种就是把动态加载资源 替换成 动态加载 dll
只是做了个简单的例子 提供个思路