WPF 自定义界面语言包

WPF 语言包

1定义语言资源

Luanguage文件夹下添加StringResource.en-US.xamlStringResource.zh-CN.xaml分别定义语言为英语和中文的语言资源文件。

StringResource.en-US.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:sys="clr-namespace:System;assembly=mscorlib">
    <sys:String x:Key="biFile">File</sys:String>
    <sys:String x:Key="biHelp">Help</sys:String>
    <sys:String x:Key="biAbout">About</sys:String>

</ResourceDictionary>

StringResource.zh-CN.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:sys="clr-namespace:System;assembly=mscorlib">
    
    <sys:String x:Key="biFile">文件</sys:String>
    <sys:String x:Key="biHelp">帮助</sys:String>
    <sys:String x:Key="biAbout">关于</sys:String>

</ResourceDictionary>

2引入资源

在App.xaml文件中将资源文件添加到App的ResourceDictionaryMergedDictionaries资源字典集合中。

<Application x:Class="Deamon.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:Deamon"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Language\StringResource.zh-CN.xaml" />
                <ResourceDictionary Source="Language\StringResource.en-US.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

3使用语言资源

1.在xaml文件中使用:

可以在任何View视图使用语言资源,但是注意:一定要使用DynamicResource进行定义。

<Window x:Class="Deamon.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:Deamon"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>

        <Menu VerticalAlignment="Top">
            <MenuItem Header="{DynamicResource biFile}"/>
            <MenuItem Header="{DynamicResource biHelp}">
                <MenuItem Header="{DynamicResource biAbout}"/>
            </MenuItem>
        </Menu>
        
    </Grid>
</Window>

2.在cs文件中使用:

可以通过查找资源来使用资源:

(string)Application.Current.Resources["biHelp"]

4语言切换

4.1在App中添加Language静态属性,当属性发生变化时,更新语言包,程序默认情况下,设置一个语言"en-US"

    public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);

            // 初始化语言
            Language = string.IsNullOrEmpty(Language) ? "en-US" : Language;
        }

        private static string language;

        public static string Language
        {
            get { return language; }
            set
            {
                if (language != value)
                {
                    language = value;
                    List<ResourceDictionary> dictionaryList = new List<ResourceDictionary>();
                    foreach (ResourceDictionary dictionary in Application.Current.Resources.MergedDictionaries)
                    {
                        dictionaryList.Add(dictionary);
                    }
                    string requestedLanguage = string.Format(@"Language\StringResource.{0}.xaml", Language);
                    ResourceDictionary resourceDictionary = dictionaryList.FirstOrDefault(d => d.Source.OriginalString.Equals(requestedLanguage));
                    if (resourceDictionary == null)
                    {
                        requestedLanguage = @"Language\StringResource.en-US.xaml";
                        resourceDictionary = dictionaryList.FirstOrDefault(d => d.Source.OriginalString.Equals(requestedLanguage));
                    }
                    if (resourceDictionary != null)
                    {
                        Application.Current.Resources.MergedDictionaries.Remove(resourceDictionary);
                        Application.Current.Resources.MergedDictionaries.Add(resourceDictionary);
                    }
                }
            }
        }
    }

4.2添加绑定命令

4.2.1绑定命令定义

 	public class Commands
    {
        /// <summary>
        /// 语言选择命令
        /// </summary>
        private static RoutedUICommand chooseLanguage = new RoutedUICommand("ChooseLanguage", "ChooseLanguage", typeof(Commands));
        public static RoutedUICommand ChooseLanguage
        {
            get { return chooseLanguage; }
        }
    }

4.2.2命令绑定

<Window x:Class="Deamon.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:Deamon"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.CommandBindings>
        <CommandBinding Command="local:Commands.ChooseLanguage" CanExecute="ChooseLanguage_CanExecute" Executed="ChooseLanguage_Executed"/>
    </Window.CommandBindings>

    <Grid>

        <Menu VerticalAlignment="Top">
            <MenuItem Header="{DynamicResource biFile}"/>
            <MenuItem Header="{DynamicResource biSetting}">
                <MenuItem Header="{DynamicResource biLanguage}">
                    <MenuItem Command="local:Commands.ChooseLanguage" CommandParameter="en" Header="English"/>
                    <MenuItem Command="local:Commands.ChooseLanguage" CommandParameter="zh" Header="中文"/>
                </MenuItem>
            </MenuItem>
            <MenuItem Header="{DynamicResource biHelp}">
                <MenuItem Header="{DynamicResource biAbout}"/>
            </MenuItem>
        </Menu>
        
    </Grid>
</Window>

4.2.3命令处理

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void ChooseLanguage_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
            e.Handled = true;
        }

        private void ChooseLanguage_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            //英文
            if (e.Parameter.ToString() == "en")
            {
                App.Language = "en-US";
            }
            //中文
            else if (e.Parameter.ToString() == "zh")
            {
                App.Language = "zh-CN";
            }
        }
    }

积跬步以至千里:) (:一阵没来由的风

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值