WPF界面多语言切换

 <ResourceDictionary>
     <ResourceDictionary.MergedDictionaries>
         <ResourceDictionary Source="en-us.xaml" />
         <ResourceDictionary Source="zh-cn.xaml" />
     </ResourceDictionary.MergedDictionaries>
 </ResourceDictionary>

 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");
 }

<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>
 private void LanguageSwitching(object sender, RoutedEventArgs e)
 {
     App.UpdateLanguage("en-us");
 }

<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>

<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>

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WPF国际化是一种用于支持多语言的开发技术,可以使应用程序自动切换不同的语言环境。WPF提供了一套机制,允许开发人员在应用程序中以一种简单而灵活的方式实现国际化。以下是如何实现WPF应用程序的自动切换语言的步骤: 1. 创建资源文件:首先,需要为每种语言创建一个资源文件。资源文件是用于存储各种语言的本地化文本信息的文件,以键值对的形式存储。例如,可以创建一个名为"Resources.resx"的默认资源文件,以及其他语言的资源文件,如"Resources.zh-CN.resx"和"Resources.en-US.resx"。 2. 添加控件标记:在XAML文件中,可以使用标记来引用资源文件中的本地化文本。例如,可以使用<TextBlock>标记来显示某个字符串,通过设置Text属性为资源文件中的键值,如Text="{x:Static resx:Resources.Hello}"。 3. 设置语言切换逻辑:在应用程序中,可以为用户提供切换语言的选项。一种常见的方法是创建一个下拉列表框,列出所有支持的语言选项。当用户选择不同的语言时,可以通过修改应用程序的CurrentUICulture属性来实现语言切换。例如,可以使用CultureInfo类将CurrentUICulture设置为选择的新语言,如Thread.CurrentThread.CurrentUICulture = new CultureInfo("zh-CN")。 4. 更新界面:当语言切换后,需要及时更新界面上显示的文本。WPF会自动根据当前的CurrentUICulture来查找并加载对应的资源文件,并将资源文件中的本地化文本应用到界面上对应的控件。 通过以上步骤,可以实现WPF应用程序的自动切换语言功能。使用WPF的国际化技术,开发人员可以轻松地为应用程序提供多语言支持,满足不同用户的语言需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值