WPF界面语言切换
举例中英文切换:
一、静态切换(每次切换需要重启应用)
1. 新建一个WPF APP(.NET Framework)项目,StaticLanguageSelect
2. 右击项目名,添加New Item,选择Resources File类型,取名为Resources.en-us.resx,把该文件拖放到Properties下,如图:
3. 使用键值对形式,在Resources.resx中存储所需的中文表示,在Resources.en-us.resx中存储所需的英文表示,如图:
注意:中文资源文件Resources.resx的Access Modifier要改成public
4. 在Properties下的Settings.settings中,新建一项,用来存储当前线程的Culture。(初始值Value为空,因为后面设置了首次启动适应系统环境。)
后面代码中用到CultureInfo类,是.NET Framework自带的支持多种语言环境和习惯的类,这可以使同一个数据适应不同地区和文化,满足处于不同地区和文化的用户。
5. 接下来在MainWindows.xaml和MainWindows.xaml.cs中写处理代码。
注意:对于.NET Framework 4.5.2版本,这样写在窗体主函数中才起作用,.NET Framework 4.6.2等高版本需要尝试写在APP.xaml.cs中。
1 <Window x:Class="StaticLanguageSelect.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:p="clr-namespace:StaticLanguageSelect.Properties"
7 xmlns:local="clr-namespace:StaticLanguageSelect"
8 mc:Ignorable="d"
9 Title="MainWindow" Height="450" Width="800">
10 <Grid>
11 <StackPanel>
12 <TextBlock Text="{x:Static p:Resources.String1}"/>
13 <Button Content="{x:Static p:Resources.SelectLanguage}" Click="Button_Click"/>
14 <Button Content="{x:Static p:Resources.Show}" Click="Button2_Click"/>
15 </StackPanel>
16 </Grid>
17 </Window>
1 using StaticLanguageSelect.Properties;
2 using System.Diagnostics;
3 using System.Globalization;
4 using System.Resources;
5 using System.Threading;
6 using System.Threading.Tasks;
7 using System.Windows;
8
9 namespace StaticLanguageSelect
10 {
11 /// <summary>
12 /// Interaction logic for MainWindow.xaml
13 /// </summary>
14 public partial class MainWindow : Window
15 {
16 private string _name;
17 private ResourceManager _currentResource;
18
19 public MainWindow()
20 {
21 var cultureName = Settings.Default.CultureName;
22
23 //如果Settings中的CultureName不为空,就实例化该种CultureInfo实例,使用它。
24 if (!string.IsNullOrEmpty(cultureName))
25 {
26 try
27 {
28 var cultureInfo = new CultureInfo(cultureName);
29 CultureInfo.DefaultThreadCurrentCulture = cultureInfo;
30 CultureInfo.DefaultThreadCurrentUICulture = cultureInfo;
31 }
32 catch (CultureNotFoundException)
33 {
34 throw;
35 }
36 }
37
38 //获取到当前线程的Culture,赋值到Settings设置中,并保存。
39 Settings.Default.CultureName = Thread.CurrentThread.CurrentUICulture.Name;
40 Settings.Default.Save();
41
42 //实例化ResourceManager,来获得当前的Resource配置。
43 _currentResource = new ResourceManager("StaticLanguageSelect.Properties.Resources", typeof(Resources).Assembly);
44
45 InitializeComponent();
46 }
47
48 private void Button_Click(object sender, RoutedEventArgs e)
49 {
50 Settings.Default.CultureName = Thread.CurrentThread.CurrentUICulture.Name == "en-US" ? "zh-CN" : "en-US";
51 Settings.Default.Save();
52
53 Task.Delay(500);
54
55 //重启WPF程序
56 Process.Start(Application.ResourceAssembly.Location);
57 Application.Current.Shutdown();
58 }
59
60 private void Button2_Click(object sender, RoutedEventArgs e)
61 {
62 _name = _currentResource.GetString("Name");
63 MessageBox.Show(_name);
64 }
65 }
66 }
6. 在弹出的子窗体中也关联当前语言:
新建子窗体ChildWindow,后台代码不用写,直接在xaml中关联资源文件中的语言键值对:
<Window x:Class="StaticLanguageSelect.ChildWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:p="clr-namespace:StaticLanguageSelect.Properties" xmlns:local="clr-namespace:StaticLanguageSelect" mc:Ignorable="d" Title="ChildWindow" Height="200" Width="300"> <Grid> <TextBlock Text="{x:Static p:Resources.String1}"/> </Grid> </Window>
主窗体按钮的点击处理程序中,实例化子窗体并显示它:
1 private void Button2_Click(object sender, RoutedEventArgs e) 2 { 3 var childWindow = new ChildWindow(); 4 childWindow.ShowDialog(); 5 }
运行程序会发现,子窗体的语言与主窗体的一致: