winform语言切换在每个窗口下面有一个.resx结尾的资源文件,在上面添加新字符串就好了;
WPF语言切换跟winform不一样的地方在于需要自己添加资源文件,并且这个资源文件可以写一个,也可以写多个。
// 英文资源文件:enus.xaml <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:sys="clr-namespace:System;assembly=mscorlib"> <sys:String x:Key ="label_text_1">Click the button to change language.</sys:String> <sys:String x:Key="button_text_1">Change</sys:String> <sys:String x:Key ="label_text_2">Click the button to change language.2222</sys:String> <sys:String x:Key="button_text_2">Change2222</sys:String> </ResourceDictionary>
// 中文资源文件:zhcn.xaml <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:sys="clr-namespace:System;assembly=mscorlib"> <sys:String x:Key="label_text_1">点击按钮更换语言</sys:String> <sys:String x:Key="button_text_1">更换</sys:String> <sys:String x:Key ="label_text_2">点击按钮更换语言.2222</sys:String> <sys:String x:Key="button_text_2">更换2222</sys:String> </ResourceDictionary>
// 项目默认资源添加:App.xaml <Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <!--绝对地址,不容易报错--> <!--<ResourceDictionary Source="pack://application:,,,/{工程名字};component{资源文件地址}"/>--> <ResourceDictionary Source="pack://application:,,,/ChangeLanguages;component/Languages/zhcn.xaml"/> <!--普通引用,跨工程容易找不到--> <!--<ResourceDictionary Source="Languages/enus.xaml"/>--> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources>
// 前台绑定使用 <Label Height="28" Content="{DynamicResource label_text_1}" VerticalAlignment="Top" x:Name="lable"/> <Button Name="btnChange" Content="{DynamicResource button_text_1}" Height="23" Width="75" Click="btnChange_Click" /> <Button Name="btn_1" Click="btn_1_Click" Height="23" Width="75" Content="{Binding ElementName=root,Path=ContentTypeProperty}"/>
// 后台绑定方法1,添加到当前页的资源字典,适合资源文件分开的情况同winform string windowCurrentLanguageFile = "Languages\enus.xaml"; private void setLanguage() { windowCurrentLanguageFile = windowCurrentLanguageFile == "Languages\enus.xaml" ? "Languages\zhcn.xaml" : "Languages\enus.xaml"; var rd = new ResourceDictionary() { Source = new Uri(windowCurrentLanguageFile, UriKind.RelativeOrAbsolute) }; if (this.Resources.MergedDictionaries.Count == 0) this.Resources.MergedDictionaries.Add(rd); else this.Resources.MergedDictionaries[0] = rd; } // 后台绑定方法2,添加到App.xaml中指定的资源,demo中就是0,App.xaml中字典中默认的条数是多少,从0开始 string windowCurrentLanguageFile = "Languages\enus.xaml"; private void setLanguage() { windowCurrentLanguageFile = windowCurrentLanguageFile == "Languages\enus.xaml" ? "Languages\zhcn.xaml" : "Languages\enus.xaml"; Application.Current.Resources.MergedDictionaries[0] = new ResourceDictionary() { Source = new Uri(windowCurrentLanguageFile, UriKind.RelativeOrAbsolute) };
//windowCurrentLanguageFile = windowCurrentLanguageFile == "enus.xaml"
// ? "zhcn.xaml"
// : "enus.xaml";
//Application.Current.Resources.MergedDictionaries[0] = new ResourceDictionary() { Source = new Uri(string.Format("pack://application:,,,/ChangeLanguages;component/Languages/{0}", windowCurrentLanguageFile), UriKind.Absolute) };
} // 后台绑定方法3,添加到App.xaml中指定的资源,通过查找来进行替换,防止报索引的错,该查找方法只支持绝对地址,2,3方法方便全局设置,即设置一次全局改变 string windowCurrentLanguageFile = "enus.xaml"; private void setLanguage() { windowCurrentLanguageFile = windowCurrentLanguageFile == "enus.xaml" ? "zhcn.xaml" : "enus.xaml"; Application.Current.Resources.MergedDictionaries.FirstOrDefault(s => s.Source.AbsoluteUri.Contains("Languages")).Source = new Uri(string.Format("pack://application:,,,/ChangeLanguages;component/Languages/{0}", windowCurrentLanguageFile), UriKind.Absolute); }