当前位置:网站首页>【WPF】通过动态/静态资源实现语言切换
【WPF】通过动态/静态资源实现语言切换
2022-07-29 07:01:00 【阿月浑子2021】
一、动态切换


1、根据需要创建语言资源(en-US和zh-CN),导入命名空间
xmlns:sys="clr-namespace:System;assembly=mscorlib
<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">
<sys:String x:Key="txtLanguage">Language</sys:String>
<sys:String x:Key="txtStandard">RF Standard</sys:String>
<sys:String x:Key="txtBandWidth">BandWidth</sys:String>
<sys:String x:Key="txtCenterFreq">CenterFreq</sys:String>
<sys:String x:Key="contChinese">Chinese</sys:String>
<sys:String x:Key="contEnglish">English</sys:String>
</ResourceDictionary><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">
<sys:String x:Key="txtLanguage">语言</sys:String>
<sys:String x:Key="txtStandard">射频标准</sys:String>
<sys:String x:Key="txtBandWidth">带宽</sys:String>
<sys:String x:Key="txtCenterFreq">中心频点</sys:String>
<sys:String x:Key="contChinese">中文</sys:String>
<sys:String x:Key="contEnglish">英文</sys:String>
</ResourceDictionary>2、在前端引用
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" Text="{DynamicResource txtLanguage}" FontSize="30" HorizontalAlignment="Center" VerticalAlignment="Center"></TextBlock>
<TextBlock Grid.Row="1" Grid.Column="0" Text="{DynamicResource txtStandard}" FontSize="30" HorizontalAlignment="Center" VerticalAlignment="Center"/>
<TextBlock Grid.Row="2" Grid.Column="0" Text="{DynamicResource txtBandWidth}" FontSize="30" HorizontalAlignment="Center" VerticalAlignment="Center"/>
<TextBlock Grid.Row="3" Grid.Column="0" Text="{DynamicResource txtCenterFreq}" FontSize="30" HorizontalAlignment="Center" VerticalAlignment="Center"/>
<ComboBox Grid.Row="0" Grid.Column="1" Name="languageCombobox" FontSize="20" Height="50" Width="200" Margin="0 0 0 10" SelectionChanged="languageCombobox_SelectionChanged">
<ComboBoxItem Content="{DynamicResource contChinese}"/>
<ComboBoxItem Content="{DynamicResource contEnglish}"/>
</ComboBox>
<ComboBox Grid.Row="1" Grid.Column="1" Name="standardCombobox" FontSize="20" Height="50" Width="200" Margin="0 0 0 10" SelectionChanged="standardCombobox_SelectionChanged"/>
<ComboBox Grid.Row="2" Grid.Column="1" Name="bandWidthCombobox" FontSize="20" Height="50" Width="200" Margin="0 0 0 10"/>
<ComboBox Grid.Row="3" Grid.Column="1" Name="centerFreCombobox" FontSize="20" Height="50" Width="200" Margin="0 0 0 10"/>
</Grid>3、语言切换,key值一样,前端会应用最新添加的资源
private void languageCombobox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (!(sender is ComboBox combobox)) return;
var languageType = "";
var selectItem = (combobox.SelectedItem as ComboBoxItem).Content;
switch (selectItem)
{
case "中文":
languageType = "zh-CN";
break;
case "英文":
languageType = "en-US";
break;
case "Chinese":
languageType = "zh-CN";
break;
case "English":
languageType = "en-US";
break;
default:
break;
}
//更换资源文件
List<ResourceDictionary> dictionaryList = new List<ResourceDictionary>();
foreach (ResourceDictionary dictionary in Application.Current.Resources.MergedDictionaries)
{
dictionaryList.Add(dictionary);
}
string requestedCulture = string.Format(@"Resource\{0}.xaml", languageType);
ResourceDictionary resourceDictionary = dictionaryList.FirstOrDefault(d => d.Source.OriginalString.Equals(requestedCulture));
if (resourceDictionary == null)
{
requestedCulture = @"Resource\zh-CN.xaml";
resourceDictionary = dictionaryList.FirstOrDefault(d => d.Source.OriginalString.Equals(requestedCulture));
}
if (resourceDictionary != null)
{
Application.Current.Resources.MergedDictionaries.Remove(resourceDictionary);
Application.Current.Resources.MergedDictionaries.Add(resourceDictionary);
}
}二、静态切换


1. 在Properties下的Resources.rexs里添加key,value,访问修饰符改为public

2.再添加一个rexs文件,添加完再重命名(添加时重命名可能会缺少文件,切换失败),同样添加key,value,修改访问修饰符3.
3.Setting里新建一条记录,给一个默认值

4.应用
前端应用的话需要引入properties的命名空间,然后赋值,例如 Text/Content={x:static res:Resources.Language },不晓得是什么原因,我前段找不到资源,就在后端赋值了
<Grid>
<TextBlock Name="texLanguage" Height="50" Width="100" FontSize="20" HorizontalAlignment="Left" Margin="30 0 0 0"/>
<ComboBox Name="languageCbb" Height="50" Width="100" FontSize="20" HorizontalAlignment="Left" Margin="200 0 0 0" SelectionChanged="ComboBox_SelectionChanged">
<ComboBoxItem Name="chineseItem"/>
<ComboBoxItem Name="englishItem"/>
</ComboBox>
</Grid>languageCbb.SelectedIndex = Thread.CurrentThread.CurrentUICulture.Name == "zh-CN" ? 0 : 1;
texLanguage.Text = Properties.Resources.Language;
chineseItem.Content = Properties.Resources.Chinese;
englishItem.Content = Properties.Resources.English;5.启动时应用语言设置,并获取对应的资源配置
public partial class App : Application
{
private ResourceManager currentResource;
public App()
{
var cultureName = Settings.Default.CultureName;
if (!string.IsNullOrEmpty(cultureName))
{
try
{
CultureInfo cultureInfo = new CultureInfo(cultureName);
CultureInfo.DefaultThreadCurrentCulture = cultureInfo;
CultureInfo.DefaultThreadCurrentUICulture = cultureInfo;
}
catch (CultureNotFoundException ex)
{
MessageBox.Show(ex.Message);
}
Settings.Default.CultureName = Thread.CurrentThread.CurrentUICulture.Name;
Settings.Default.Save();
//获得当前的Resource配置
currentResource = new ResourceManager("Language.Properties.Resources", typeof(Properties.Resources).Assembly);
}
}
}6.切换语言时修改配置并重启
private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (!(sender is ComboBox combobox)) return;
if(e.RemovedItems.Count != 0)
{
Properties.Settings.Default.CultureName = Thread.CurrentThread.CurrentUICulture.Name == "zh-CN" ? "en-US" : "zh-CN";
Properties.Settings.Default.Save();
Task.Delay(500);
Process.Start(Application.ResourceAssembly.Location);
Application.Current.Shutdown();
}
}边栏推荐
猜你喜欢

Summary of OCR optical character recognition methods

QT basic day 2 (2) QT basic components: button class, layout class, output class, input class, container and other individual examples

Operator3-设计一个operator

JS chicken laying eggs and egg laying chickens. Who appeared earlier, object or function? Is function an instance of function?

A long article --- in-depth understanding of synchronized

JS break and continue and return keywords

2-统一返回类DTO对象

Paper reading (62):pointer networks

Excel文件读写(创建与解析)

同步/异步、阻塞/非阻塞 与 IO
随机推荐
我,28岁,测试员,10月无情被辞:想给还在学测试 的人提个醒......
能在SQL 语句中 指定 内存参数吗?
logback日志级别简介说明
女研究生做“思维导图”与男友吵架!网友:吵架届的“内卷之王”....
LevelFilter简介说明
Paper reading (62):pointer networks
Gin Middleware
SpingBoot整合Quartz框架实现动态定时任务(支持实时增删改查任务)
Unity发送Post请求给GoLang服务端解析并返回
彻底搞懂kubernetes调度框架与插件
暑期总结(二)
Other basic monitoring items of ZABBIX
用户列表 圆形头像并跟随小板块
Full process flow of CMOS chip manufacturing
5-integrate swagger2
个人博客系统(附源码)
fillder使用
Latest 10 billion quantitative private placement list
Can I specify memory parameters in SQL statements?
Interface test actual project 03: execute test cases