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>
相关推荐
冰帝海岸30 分钟前
01-spring security认证笔记
java·笔记·spring
活宝小娜1 小时前
vue不刷新浏览器更新页面的方法
前端·javascript·vue.js
世间万物皆对象1 小时前
Spring Boot核心概念:日志管理
java·spring boot·单元测试
程序视点1 小时前
【Vue3新工具】Pinia.js:提升开发效率,更轻量、更高效的状态管理方案!
前端·javascript·vue.js·typescript·vue·ecmascript
coldriversnow1 小时前
在Vue中,vue document.onkeydown 无效
前端·javascript·vue.js
我开心就好o1 小时前
uniapp点左上角返回键, 重复来回跳转的问题 解决方案
前端·javascript·uni-app
没书读了1 小时前
ssm框架-spring-spring声明式事务
java·数据库·spring
小二·2 小时前
java基础面试题笔记(基础篇)
java·笔记·python
开心工作室_kaic2 小时前
ssm161基于web的资源共享平台的共享与开发+jsp(论文+源码)_kaic
java·开发语言·前端
刚刚好ā2 小时前
js作用域超全介绍--全局作用域、局部作用、块级作用域
前端·javascript·vue.js·vue