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 }
运行程序会发现,子窗体的语言与主窗体的一致:


WPF界面语言切换的更多相关文章
- Matlab界面语言切换,自由显示中文或英文语言
Matlab界面语言切换,自由显示中文或英文语言分享给大家,Matlab是一款商业数学软件,广泛使用于算法的开发.数据发现和数值计算等.不同用户对Matlab显示的语言需求也不一样,一用户习惯使用中文 ...
- WPF页面切换及弹窗
WPF页面切换及弹窗 结构如图: 效果如图: 代码如下: xaml <Window x:Class="PageChange.MainWindow" xmlns="h ...
- WPF语言切换,国际化
winform语言切换在每个窗口下面有一个.resx结尾的资源文件,在上面添加新字符串就好了: WPF语言切换跟winform不一样的地方在于需要自己添加资源文件,并且这个资源文件可以写一个,也可以写 ...
- WPF 页面切换效果
原文:WPF 页面切换效果 最近做一个有页面切换的吧.. 我觉得这个功能是比较基础的吧.. 在网上百度了一下.. 用NavigationWindow的比较好.. 因为Demo中是带了淡入淡出的页面效果 ...
- WPF 主题切换(Z)
using System; using System.Windows; using Assergs.Windows; namespace XMLSpy.WPF.Util{ /// <summar ...
- 【转】VS2012 中文版转英文版 英文版转中文版 界面语言切换
[1]下载VS2012的语言包,各种语言包都有,下载对应的即可. 微软官网衔接地址:vs2012 语言包 http://www.microsoft.com/zh-CN/download/detail ...
- wpf图片切换,幻灯效果
xaml: <Window x:Class="WpfApplication1.PicShow" xmlns="http://schemas.microsoft.co ...
- WPF页面切换
XAML <Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft. ...
- eclipse界面语言的切换方法
很久以前在我一个朋友的blog上看到过关于eclipse中英文语言界面切换的方法,觉得挺不错,后来自己也用过几次 现在想想,这个方法真的很不错,刚才又在自己机器上做了几次试验,发现eclipse事实上 ...
随机推荐
- pandas目录
pandas目录 1 Lesson1--Pandas是什么 2 Lesson2--Pandas库下载和安装 3 Lesson3--Pandas Series结构 4 Lesson4--Pandas D ...
- LCT 入门
这是一份 \(\rm LCT\) 入门总结. 关于 \(\rm LCT\) 的复杂度这里不会提及,只会记录 \(\rm LCT\) 的基本操作和经典例题,但神奇的 \(\rm LCT\) 虽然常数巨大 ...
- Win10 提示凭证不工作问题
感谢大佬:https://cloud.tencent.com/developer/article/1337081 在公司局域网远程自己计算机的时候,突然无法远程了,提示"您的凭据不工作 之前 ...
- 关于CALayer的疑惑
- LNMP架构——源码编译安装
LNMP架构--源码编译安装 1.编译安装nginx服务 2.编译安装mysql服务 3.编译安装php解析环境 1.编译安装nginx服务: systemctl stop firewalld sys ...
- 剑指Offer系列_30_包含min函数的栈
以空间换时间: package leetcode.sword_to_offfer.day01; import java.util.Stack; /** * 定义栈的数据结构,请在该类型中实现一个能够得 ...
- linux c 线程相关函数
线程相关函数(1)-pthread_create(), pthread_join(), pthread_exit(), pthread_cancel() 创建取消线程 一. pthread_creat ...
- AppiumForWin安装
尝试安装Windows版本的Appium 参考:http://www.cnblogs.com/fnng/p/4540731.html 第一步:安装node https://nodejs.org/en/ ...
- CentOS7利用yum缓存搭建本地源
CentOS7利用yum缓存搭建本地源 环境说明 [root@localhost ~]# cat /etc/redhat-release CentOS Linux release 7.6.1810 ( ...
- 从零开始, 开发一个 Web Office 套件(4):新的问题—— z-index
<从零开始, 开发一个 Web Office 套件>系列博客目录 这是一个系列博客, 最终目的是要做一个基于HTML Canvas 的, 类似于微软 Office 的 Web Office ...