当前位置:   article > 正文

WPF实现多语言切换_wpf多语言切换

wpf多语言切换

Demo

代码实例:https://gitee.com/chenheze90/L13_WPFMultipleLanguage

功能点说明

功能的关键在于:创建两个语言字典(中英文各一个),key和value键值对的那种
切换语言的时候,切换字典就行。
前端绑定key就行,自动显示value。
这里有一个很重要的问题,字段切换了,前端怎么会更新?用DynamicResource绑定就行,它会自动更新资源。

构建的关键代码如下:
Application.Current.Resources.MergedDictionaries.Remove(resourceDictionary);
Application.Current.Resources.MergedDictionaries.Add(resourceDictionary);

实战

首先,创建一个wpf项目:WPFMultipleLanguage.csproj
在这里插入图片描述

添加资源(语言字典)

增加两个资源文件:zh-cn.xaml和en-us.xaml
选中项目-右键点击-添加-资源字典
在这里插入图片描述
zh-cn.xaml资源字典的代码如下

在这里插入图片描述

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:WPFMultipleLanguage"
                    xmlns:s="clr-namespace:System;assembly=mscorlib">
    <s:String x:Key="OK">确认</s:String>
    <s:String x:Key="Cancle">取消</s:String>
</ResourceDictionary>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

en-us.xaml的代码如下
在这里插入图片描述

<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>
</ResourceDictionary>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

这样,资源字典就完成了
资源字典添加还不算完成,必须将字典添加到系统
打开App.xaml
在这里插入图片描述
中间加上字典声明

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

注意:这边的"en-us.xaml"其实是一个路径
如下所示
在这里插入图片描述

生成界面

然后接下来增加窗体添和控件
在这里插入图片描述
代码如下

<Window x:Class="WPFMultipleLanguage.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WPFMultipleLanguage"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <StackPanel>
        <StackPanel.Resources>
            <Style TargetType="Button">
                <Setter Property="Width" Value="120"></Setter>
                <Setter Property="Height" Value="25"></Setter>
            </Style>
        </StackPanel.Resources>
        <Button Content="中文"></Button>
        <Button Content="英语"></Button>
        <Button Content="{DynamicResource OK}"></Button>
        <Button Content="{DynamicResource Cancle}"></Button>
    </StackPanel>
</Window>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

注意:最后两个按钮一定要用DynamicResource绑定,绑定到资源

配置设置

配置设置的目的是为了保留语言设置
在 App.config 中添加你想要使用的键值对

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="Language" value="en-us" />
  </appSettings>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
    </startup>
</configuration>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

后台代码实现

添加引用
在这里插入图片描述
打开App.xaml的后台代码,添加UpdateLanguage(string lan)方法

using System.Configuration;
using System.Linq;
using System.Windows;

namespace WPFMultipleLanguage
{
    /// <summary>
    /// App.xaml 的交互逻辑
    /// </summary>
    public partial class App : Application
    {
        //更换语言
        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");
        }
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

到MainWindow.xaml前端代码,给两个按钮增加事件

        <Button Content="中文" Click="Button_Click"></Button>
        <Button Content="英语" Click="Button_Click_1"></Button>
  • 1
  • 2

事件对应的后台代码如下

using System.Windows;

namespace WPFMultipleLanguage
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            App.UpdateLanguage("zh-cn");
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            App.UpdateLanguage("en-us");
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

完成之后运行代码,就可以看到多语音切换的效果了

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

闽ICP备14008679号