当前位置:网站首页>【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();
}
}
边栏推荐
猜你喜欢
以太网接口介绍
My personal website doesn't allow access to wechat, so I did this
Excel文件读写(创建与解析)
MySQL如何把行转换为列?
Some learning and understanding of vintage analysis
WPF nested layout case
Comparison of advantages between can & canfd integrated test analysis software lkmaster and PCA Explorer 6 analysis software
2022-07-28: what is the output of the following go language code? A:AA; B:AB; C:BA; D:BB。 package main import ( “fmt“ ) func main() { f
Docker最新超详细教程——Docker创建运行MySQL并挂载
Job 7.28 file IO and standard IO
随机推荐
gcc/g++的使用
cdc source能读完MySqlSnapshotSplit 就退出嘛
After 4 years of development and 13K, if you want to change to automated testing, can your salary still rise···
Remote invocation of microservices
logback中RollingFileAppender属性简介说明
Cvpr2021 | multi view stereo matching based on self supervised learning (cvpr2021)
JS break and continue and return keywords
How does MySQL convert rows to columns?
Clock tree synthesis (I)
Operator3 - design an operator
Vite3.0都发布了,你还能卷得动吗(新特性一览)
Scala 高阶(十):Scala中的异常处理
Excel文件读写(创建与解析)
Fillder use
Tp6 use protobuf
【OpenGL】着色器(Shader)的使用
logback 中FileAppender具有什么功能呢?
如何与斯堪尼亚SCANIA建立EDI连接?
论文阅读 (62):Pointer Networks
Section 7 - compilation of programs (preprocessing operations) + links