WPF界面多语言切换

cs 复制代码
 <ResourceDictionary>
     <ResourceDictionary.MergedDictionaries>
         <ResourceDictionary Source="en-us.xaml" />
         <ResourceDictionary Source="zh-cn.xaml" />
     </ResourceDictionary.MergedDictionaries>
 </ResourceDictionary>
cs 复制代码
 public static void UpdateLanguage(string lan)
 {
     // 获取配置
     string requestedLanguage = $"{lan}.xaml";
     ResourceDictionary resourceDictionary = Application.Current.Resources.MergedDictionaries.FirstOrDefault(d => d.Source.OriginalString.Equals(requestedLanguage));
     Current.Resources.MergedDictionaries.Remove(resourceDictionary);
     Current.Resources.MergedDictionaries.Add(resourceDictionary);

     // 保存配置
     Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
     ConfigurationManager.AppSettings["Language"] = lan;
     config.Save(ConfigurationSaveMode.Modified);
     //刷新
     ConfigurationManager.RefreshSection("appSettings");
 }
html 复制代码
<StackPanel Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
    <!--<TextBlock Text="用户名:"  Margin="0,5,0,0"/>-->
    <TextBlock Text="{DynamicResource Username}"  Margin="0,5,0,0"/>
    <TextBox x:Name="txtUsername" Width="200" Height="30" Margin="0,0,15,0"/>
</StackPanel>


<StackPanel Grid.Row="2" Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
    <TextBlock Text="{DynamicResource Password}"  Margin="0,5,0,0"/>
    <PasswordBox x:Name="txtPassword" Width="200" Height="30" Margin="0,0,15,0"/>
    
</StackPanel>
<StackPanel Grid.Row="3" HorizontalAlignment="Center" VerticalAlignment="Center">
    <Button Content="{DynamicResource Login}"  Background="#0047AB" Foreground="White"  HorizontalAlignment="Right"  Click="LoginButton_Click" Width="200" Height="30" Margin="30,0,0,0"/>
</StackPanel>
cs 复制代码
 private void LanguageSwitching(object sender, RoutedEventArgs e)
 {
     App.UpdateLanguage("en-us");
 }
cs 复制代码
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:s="clr-namespace:System;assembly=mscorlib">
    <s:String x:Key="OK">Sure</s:String>
    <s:String x:Key="Cancle">NoSure</s:String>
    <s:String x:Key="Username">UserName:</s:String>
    <s:String x:Key="Password">PassWord:</s:String>
    <s:String x:Key="Login">Login</s:String>
    <s:String x:Key="LanS">简体中文</s:String>
</ResourceDictionary>
cs 复制代码
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
           
                    xmlns:s="clr-namespace:System;assembly=mscorlib">
    <s:String x:Key="OK">确认</s:String>
    <s:String x:Key="Cancle">取消</s:String>
    <s:String x:Key="Username">用户名:</s:String>
    <s:String x:Key="Password">密码:</s:String>
    <s:String x:Key="Login">登录</s:String>
    <s:String x:Key="LanS">English</s:String>
</ResourceDictionary>
相关推荐
雷神乐乐4 分钟前
File.separator与File.separatorChar的区别
java·路径分隔符
小刘|9 分钟前
《Java 实现希尔排序:原理剖析与代码详解》
java·算法·排序算法
逊嘘28 分钟前
【Java语言】抽象类与接口
java·开发语言·jvm
morris13135 分钟前
【SpringBoot】Xss的常见攻击方式与防御手段
java·spring boot·xss·csp
别拿曾经看以后~37 分钟前
【el-form】记一例好用的el-input输入框回车调接口和el-button按钮防重点击
javascript·vue.js·elementui
川石课堂软件测试43 分钟前
性能测试|docker容器下搭建JMeter+Grafana+Influxdb监控可视化平台
运维·javascript·深度学习·jmeter·docker·容器·grafana
JerryXZR1 小时前
前端开发中ES6的技术细节二
前端·javascript·es6
七星静香1 小时前
laravel chunkById 分块查询 使用时的问题
java·前端·laravel
Jacob程序员1 小时前
java导出word文件(手绘)
java·开发语言·word
ZHOUPUYU1 小时前
IntelliJ IDEA超详细下载安装教程(附安装包)
java·ide·intellij-idea