当前位置:   article > 正文

WPF界面多语言切换

WPF界面多语言切换

  1. <ResourceDictionary>
  2. <ResourceDictionary.MergedDictionaries>
  3. <ResourceDictionary Source="en-us.xaml" />
  4. <ResourceDictionary Source="zh-cn.xaml" />
  5. </ResourceDictionary.MergedDictionaries>
  6. </ResourceDictionary>

  1. public static void UpdateLanguage(string lan)
  2. {
  3. // 获取配置
  4. string requestedLanguage = $"{lan}.xaml";
  5. ResourceDictionary resourceDictionary = Application.Current.Resources.MergedDictionaries.FirstOrDefault(d => d.Source.OriginalString.Equals(requestedLanguage));
  6. Current.Resources.MergedDictionaries.Remove(resourceDictionary);
  7. Current.Resources.MergedDictionaries.Add(resourceDictionary);
  8. // 保存配置
  9. Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
  10. ConfigurationManager.AppSettings["Language"] = lan;
  11. config.Save(ConfigurationSaveMode.Modified);
  12. //刷新
  13. ConfigurationManager.RefreshSection("appSettings");
  14. }

  1. <StackPanel Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
  2. <!--<TextBlock Text="用户名:" Margin="0,5,0,0"/>-->
  3. <TextBlock Text="{DynamicResource Username}" Margin="0,5,0,0"/>
  4. <TextBox x:Name="txtUsername" Width="200" Height="30" Margin="0,0,15,0"/>
  5. </StackPanel>
  6. <StackPanel Grid.Row="2" Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
  7. <TextBlock Text="{DynamicResource Password}" Margin="0,5,0,0"/>
  8. <PasswordBox x:Name="txtPassword" Width="200" Height="30" Margin="0,0,15,0"/>
  9. </StackPanel>
  10. <StackPanel Grid.Row="3" HorizontalAlignment="Center" VerticalAlignment="Center">
  11. <Button Content="{DynamicResource Login}" Background="#0047AB" Foreground="White" HorizontalAlignment="Right" Click="LoginButton_Click" Width="200" Height="30" Margin="30,0,0,0"/>
  12. </StackPanel>
  1. private void LanguageSwitching(object sender, RoutedEventArgs e)
  2. {
  3. App.UpdateLanguage("en-us");
  4. }

  1. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  2. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  3. xmlns:s="clr-namespace:System;assembly=mscorlib">
  4. <s:String x:Key="OK">Sure</s:String>
  5. <s:String x:Key="Cancle">NoSure</s:String>
  6. <s:String x:Key="Username">UserName:</s:String>
  7. <s:String x:Key="Password">PassWord:</s:String>
  8. <s:String x:Key="Login">Login</s:String>
  9. <s:String x:Key="LanS">简体中文</s:String>
  10. </ResourceDictionary>

  1. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  2. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  3. xmlns:s="clr-namespace:System;assembly=mscorlib">
  4. <s:String x:Key="OK">确认</s:String>
  5. <s:String x:Key="Cancle">取消</s:String>
  6. <s:String x:Key="Username">用户名:</s:String>
  7. <s:String x:Key="Password">密码:</s:String>
  8. <s:String x:Key="Login">登录</s:String>
  9. <s:String x:Key="LanS">English</s:String>
  10. </ResourceDictionary>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/神奇cpp/article/detail/963704
推荐阅读
相关标签
  

闽ICP备14008679号