最近在用wpf做一个项目,需要提供中文和俄文切换,这是效果图
1.在程序根目录下新建两个资源词典文件 ,注意加上:xmlns:sys="clr-namespace:System;assembly=mscorlib”
中文:“zh-CN.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"
xmlns:local="clr-namespace:JNTApp">
<!--登录-->
<sys:String x:Key="WinLogin">登录</sys:String>
<sys:String x:Key="txtAccount">账号:</sys:String>
<sys:String x:Key="txtPasswd">密码:</sys:String>
<sys:String x:Key="cobChoose">选择语言:</sys:String>
<sys:String x:Key="chkSave">记住密码</sys:String>
<sys:String x:Key="chkAuto">自动登录</sys:String>
<sys:String x:Key="btnLogin">登录</sys:String>
<sys:String x:Key="btnCancel">取消</sys:String>
<sys:String x:Key="cobCh">中文</sys:String>
<sys:String x:Key="cobEw">俄文</sys:String>
</ResourceDictionary>
俄文:“ru.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"
xmlns:local="clr-namespace:JNTApp">
<!--войти-->
<sys:String x:Key="WinLogin">войти</sys:String>
<sys:String x:Key="txtAccount">Логин:</sys:String>
<sys:String x:Key="txtPasswd">Пароль:</sys:String>
<sys:String x:Key="cobChoose">выбор языка:</sys:String>
<sys:String x:Key="chkSave">запомнить Пароль</sys:String>
<sys:String x:Key="chkAuto">автоматический вход</sys:String>
<sys:String x:Key="btnLogin">войти</sys:String>
<sys:String x:Key="btnCancel">отменить</sys:String>
<sys:String x:Key="cobCh">китайский язык</sys:String>
<sys:String x:Key="cobEw">русский язык</sys:String>
</ResourceDictionary>
定义完成之后,在界面引用; Text="{DynamicResource txtAccount}"
2.在App.xaml加上
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ru.xaml"/>
<ResourceDictionary Source="zh-CN.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
3.在App.xaml.cs加一个方法
{
currApp.StartupUri = new Uri("SignWindow.xaml", UriKind.RelativeOrAbsolute);
LoadLanguage();
}
private void LoadLanguage()
{
CultureInfo currentCultureInfo = CultureInfo.CurrentCulture;
ResourceDictionary langRd = null;
try
{
langRd =
Application.LoadComponent(
new Uri(currentCultureInfo.Name + ".xaml", UriKind.Relative)) as ResourceDictionary;
}
catch
{
}
if (langRd != null)
{
if (this.Resources.MergedDictionaries.Count > 0)
{
this.Resources.MergedDictionaries.Clear();
}
this.Resources.MergedDictionaries.Add(langRd);
}
}
{
InitializeComponent();
currentLang = "zh-CN";
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("zh-CN");
/// 语言选择
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void cobChoose_DropDownClosed(object sender, EventArgs e)
{
string language = this.cobChoose.Text;
string languageType = "";
if (!string.IsNullOrEmpty(language))
{
switch (language)
{
case "中文": languageType = "zh-CN"; break;
case "俄文": languageType = "ru"; break;
case "китайский язык": languageType = "zh-CN"; break;
case "русский язык": languageType = "ru"; break;
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("zh-CN");
ChangeLanguage.change(languageType, this);
if (MainWindow != null)
{
ChangeLanguage.change(languageType, MainWindow);
}
currentLang = languageType;
Variable.LanguageSeesion = currentLang;
}
}
{
public static void change(string currentLang,Window w)
{
System.Windows.ResourceDictionary langRd = null;
try
{
langRd =
System.Windows.Application.LoadComponent(
new Uri(currentLang + ".xaml", UriKind.Relative)) as ResourceDictionary;
}
catch
{
}
if (langRd != null)
{
if (w.Resources.MergedDictionaries.Count > 0)
{
w.Resources.MergedDictionaries.Clear();
}
w.Resources.MergedDictionaries.Add(langRd);
}
public static void changePage(string currentLang)
{
System.Windows.ResourceDictionary langRd = null;
try
{
langRd =
System.Windows.Application.LoadComponent(
new Uri(currentLang + ".xaml", UriKind.Relative)) as ResourceDictionary;
}
catch
{
}
if (langRd != null)
{
Application.Current.Resources.MergedDictionaries.Clear();
Application.Current.Resources.MergedDictionaries.Add(langRd);
}
第一次写博客,只管贴代码,有些地方可能没说清楚,不过看代码应该能看清楚吧