1、新建两个资源字典文件zh-CN.xaml和en-US.xaml,分别存储中文模板和英文模板

(1) zh-CN.xaml

语言:英语中文确定这是语言切换测试文本内容

(2) en-US.xaml

Language:EnglishChineseOKThis is the language switching test text content.

2、在App.xaml文件内,引入默认使用的语言对应的资源字典,在此默认语言为中文

3、在MainWindow.xaml中添加中英文单选按钮和切换按钮,以及测试文本;并使用{DynamicResource x:Key}动态引用资源字典

4、MainWindow.xaml.cs中添加切换逻辑

namespace LanguageChangeDemo{/// /// Interaction logic for MainWindow.xaml/// public partial class MainWindow : Window{string selectItem = "";string languageType = "";public MainWindow(){InitializeComponent();}private void RadioButton_Checked(object sender, RoutedEventArgs e){RadioButton radioButton = sender as RadioButton;if (radioButton.IsChecked == true){selectItem = radioButton.Content.ToString();switch (selectItem){case "中文":languageType = "zh-CN";Change();break;case "英语":languageType = "en-US";Change();break;case "Chinese":languageType = "zh-CN";Change();break;case "English":languageType = "en-US";Change();break;default:break;}}}private void Button_Click(object sender, RoutedEventArgs e){改版前//if (languageType != null)//{//ResourceDictionary langRd = null;//try//{////根据名字载入语言文件//langRd = Application.LoadComponent(new Uri(languageType + ".xaml", UriKind.Relative)) as ResourceDictionary;//}//catch (Exception e2)//{//MessageBox.Show(e2.Message);//}//if (langRd != null)//{////如果已使用其他语言,先清空//if (this.Resources.MergedDictionaries.Count > 0)//{//this.Resources.MergedDictionaries.Clear();//}//this.Resources.MergedDictionaries.Add(langRd);//}//}}public void Change(){if (languageType != null){ResourceDictionary langRd = null;try{//根据名字载入语言文件langRd = Application.LoadComponent(new Uri(languageType + ".xaml", UriKind.Relative)) as ResourceDictionary;}catch (Exception e2){MessageBox.Show(e2.Message);}if (langRd != null){//如果已使用其他语言,先清空if (this.Resources.MergedDictionaries.Count > 0){this.Resources.MergedDictionaries.Clear();}this.Resources.MergedDictionaries.Add(langRd);}}}}}

5、效果