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>
相关推荐
山峰哥6 分钟前
现代 C++ 的炼金术:铸就高性能与高可维护性的工程实践
java·开发语言·前端·数据结构·c++
27669582929 分钟前
闪购商家端 mtgsig
java·python·c#·node·c·mtgsig·mtgsig1.2
AndrewHZ11 分钟前
【GIS数据处理】什么是Cesium?从零入门Web端三维地理可视化工具
javascript·gis·web开发·cesium·gis前端·三维地理可视化
GISer_Jing13 分钟前
Next.js 15 全栈开发实战指南
开发语言·javascript·ecmascript
JIngJaneIL15 分钟前
基于Java在线考试管理系统(源码+数据库+文档)
java·开发语言·数据库·vue.js·spring boot
凌康ACG15 分钟前
c++使用quickjs执行JavaScript
javascript·c++·quickjs
雨落秋垣16 分钟前
SpringCache 缓存:注意事项、问题解决与优化策略
java·spring·缓存
计算机毕设指导618 分钟前
基于微信小程序的篮球场馆预订系统【源码文末联系】
java·spring boot·mysql·微信小程序·小程序·tomcat·maven
JIngJaneIL19 分钟前
基于Java音乐管理系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot
毕设源码-朱学姐21 分钟前
【开题答辩全过程】以 驾校信息管理系统为例,包含答辩的问题和答案
java·spring boot