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>
相关推荐
HanhahnaH2 分钟前
Spring集合注入Bean
java·spring
未定义.2218 分钟前
电子削铅笔刀顺序图详解:从UML设计到PlantUML实现
java·软件工程·uml
小墨宝16 分钟前
js 生成pdf 并上传文件
前端·javascript·pdf
雾月5525 分钟前
LeetCode 1292 元素和小于等于阈值的正方形的最大边长
java·数据结构·算法·leetcode·职场和发展
www_pp_1 小时前
# 构建词汇表:自然语言处理中的关键步骤
前端·javascript·自然语言处理·easyui
YuShiYue1 小时前
pnpm monoreop 打包时 node_modules 内部包 typescript 不能推导出类型报错
javascript·vue.js·typescript·pnpm
24k小善1 小时前
Flink TaskManager详解
java·大数据·flink·云计算
想不明白的过度思考者1 小时前
Java从入门到“放弃”(精通)之旅——JavaSE终篇(异常)
java·开发语言
天天扭码2 小时前
总所周知,JavaScript中有很多函数定义方式,如何“因地制宜”?(ˉ﹃ˉ)
前端·javascript·面试
一个专注写代码的程序媛2 小时前
为什么vue的key值,不用index?
前端·javascript·vue.js