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

積跬步以至千里:) (:一陣沒來由的風

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章