当前位置:   article > 正文

WPF应用程序(.Net Framework 4.8) 国际化

WPF应用程序(.Net Framework 4.8) 国际化

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

(1) zh-CN.xaml

  1. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  2.                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  3.                     xmlns:sys="clr-namespace:System;assembly=mscorlib">
  4.     <sys:String x:Key="labelLanguage">语言:</sys:String>
  5.     <sys:String x:Key="comboEnglish">英语</sys:String>
  6.     <sys:String x:Key="comboChinese">中文</sys:String>
  7.     <sys:String x:Key="BtnContent">确定</sys:String>
  8.     <sys:String x:Key="textboxContent">这是语言切换测试文本内容</sys:String>
  9. </ResourceDictionary>

(2) en-US.xaml

  1. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  2.                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  3.                     xmlns:sys="clr-namespace:System;assembly=mscorlib">
  4.     <sys:String x:Key="labelLanguage">Language:</sys:String>
  5.     <sys:String x:Key="comboEnglish">English</sys:String>
  6.     <sys:String x:Key="comboChinese">Chinese</sys:String>
  7.     <sys:String x:Key="BtnContent">OK</sys:String>
  8.     <sys:String x:Key="textboxContent">This is the language switching test text content.</sys:String>
  9. </ResourceDictionary>

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

  1. <Application x:Class="LanguageChangeDemo.App"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:local="clr-namespace:LanguageChangeDemo"
  5. StartupUri="MainWindow.xaml">
  6. <Application.Resources>
  7. <ResourceDictionary>
  8. <ResourceDictionary.MergedDictionaries>
  9. <ResourceDictionary Source="zh-CN.xaml" />
  10. </ResourceDictionary.MergedDictionaries>
  11. </ResourceDictionary>
  12. </Application.Resources>
  13. </Application>

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

  1. <Window x:Class="LanguageChangeDemo.MainWindow"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  5. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  6. xmlns:local="clr-namespace:LanguageChangeDemo"
  7. mc:Ignorable="d"
  8. Title="{DynamicResource International}" Height="250" Width="395" WindowStartupLocation="CenterScreen">
  9. <Grid>
  10. <Label Content="{DynamicResource labelLanguage}" HorizontalAlignment="Left" Margin="97,54,0,0" VerticalAlignment="Top" HorizontalContentAlignment="Center" VerticalContentAlignment="Center"/>
  11. <RadioButton x:Name="cn" Content="{DynamicResource comboChinese}" GroupName="LanguangeChoice" HorizontalAlignment="Left" Margin="169,60,0,0" VerticalAlignment="Top" HorizontalContentAlignment="Left" VerticalContentAlignment="Center" Checked="RadioButton_Checked" Tag="0"/>
  12. <RadioButton x:Name="en" Content="{DynamicResource comboEnglish}" GroupName="LanguangeChoice" HorizontalAlignment="Left" Margin="261,60,0,0" VerticalAlignment="Top" HorizontalContentAlignment="Left" VerticalContentAlignment="Center" Checked="RadioButton_Checked" Tag="1"/>
  13. <Button Visibility="Hidden" Content="{DynamicResource BtnContent}" HorizontalAlignment="Left" Margin="274,54,0,0" VerticalAlignment="Top" Height="25" Width="60" Click="Button_Click"/>
  14. <TextBox Text="{DynamicResource textboxContent}" HorizontalAlignment="Center" Margin="0,132,0,0" TextWrapping="Wrap" VerticalAlignment="Top" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Width="395" BorderThickness="0"/>
  15. </Grid>
  16. </Window>

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

  1. namespace LanguageChangeDemo
  2. {
  3. /// <summary>
  4. /// Interaction logic for MainWindow.xaml
  5. /// </summary>
  6. public partial class MainWindow : Window
  7. {
  8. string selectItem = "";
  9. string languageType = "";
  10. public MainWindow()
  11. {
  12. InitializeComponent();
  13. cn.IsChecked = true;
  14. }
  15. private void RadioButton_Checked(object sender, RoutedEventArgs e)
  16. {
  17. RadioButton radioButton = sender as RadioButton;
  18. if (radioButton.IsChecked == true)
  19. {
  20. string tag = radioButton.Tag.ToString().Trim();
  21. //selectItem = radioButton.Content.ToString();
  22. switch (tag)
  23. {
  24. case "0":
  25. languageType = "zh-CN";
  26. Change();
  27. break;
  28. case "1":
  29. languageType = "en-US";
  30. Change();
  31. break;
  32. default:
  33. break;
  34. }
  35. }
  36. }
  37. private void Button_Click(object sender, RoutedEventArgs e)
  38. {
  39. 改版前
  40. //if (languageType != null)
  41. //{
  42. // ResourceDictionary langRd = null;
  43. // try
  44. // {
  45. // //根据名字载入语言文件
  46. // langRd = Application.LoadComponent(new Uri(languageType + ".xaml", UriKind.Relative)) as ResourceDictionary;
  47. // }
  48. // catch (Exception e2)
  49. // {
  50. // MessageBox.Show(e2.Message);
  51. // }
  52. // if (langRd != null)
  53. // {
  54. // //如果已使用其他语言,先清空
  55. // if (this.Resources.MergedDictionaries.Count > 0)
  56. // {
  57. // this.Resources.MergedDictionaries.Clear();
  58. // }
  59. // this.Resources.MergedDictionaries.Add(langRd);
  60. // }
  61. //}
  62. }
  63. public void Change()
  64. {
  65. if (languageType != null)
  66. {
  67. ResourceDictionary langRd = null;
  68. try
  69. {
  70. //根据名字载入语言文件
  71. langRd = Application.LoadComponent(new Uri(languageType + ".xaml", UriKind.Relative)) as ResourceDictionary;
  72. }
  73. catch (Exception e2)
  74. {
  75. MessageBox.Show(e2.Message);
  76. }
  77. if (langRd != null)
  78. {
  79. //如果已使用其他语言,先清空
  80. if (this.Resources.MergedDictionaries.Count > 0)
  81. {
  82. this.Resources.MergedDictionaries.Clear();
  83. }
  84. Application.Current.Resources.MergedDictionaries[0] = langRd;
  85. }
  86. ///后台获取资源方式
  87. var v = FindResource("BtnContent");
  88. }
  89. //
  90. }
  91. }
  92. }

5、效果 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/427817
推荐阅读