20200526小記——C#WPF資源詞典XAML實現多種語言

C#中實現要多種語言,網上很多介紹都是利用resx資源文件以及C#自帶的CultureInfo類來實現,而且resx資源文件對於圖像、圖標、音頻和視頻等資源都可以更新替換,實現更多的內容。而對於某些程序來說,可能只要簡單一些文字翻譯即可,這裏介紹一種利用資源詞典實現多語言支持。

首先,在主項目下新建文件夾Language,在Language文件夾下建立兩個資源詞典文件cn.xaml和en.xaml作爲用到的文本做多種語言版本。並在XAML文件中編寫你所用到的文本。

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:UI.Language"
                    xmlns:sys="clr-namespace:System;assembly=mscorlib">
    <sys:String x:Key="Project">項目</sys:String>
</ResourceDictionary>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:UI.Language"
                    xmlns:sys="clr-namespace:System;assembly=mscorlib">
    <sys:String x:Key="Project"Project</sys:String>
</ResourceDictionary>

注意兩處用到的文本位置儘量對應,方便查看對照。

其次,在App.xaml文件中添加引用

<Application x:Class="UI.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:UI"      
             Exit="Application_Exit"
             DispatcherUnhandledException="Application_DispatcherUnhandledException">

    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/UI;component/Language/cn.xaml"/>
                <ResourceDictionary Source="pack://application:,,,/UI;component/Language/en.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
    
</Application>

可以調整cn.xaml和en.xaml的位置,由於兩者有相同的屬性和對應值,界面上顯示的會是後一個(這裏是en.xaml,需要修改默認的資源可以調整)

設置好資源後,可以在配置文件中設置你的默認語言,以及自己設計相應的語言選擇界面。麻煩的一點是WPF界面上控件的修改需要重繪控件才行,所以1每次更換語言重啓2在啓動頁更換語言。

更換語言後如何更新呢?我是在Language文件夾下新建一個類Language.cs,每次更新時調用一下下方這個函數就行(using System.Windows)

public static void Updatelanguage(string lang) {
            List<ResourceDictionary> dictionaryList = new List<ResourceDictionary>();
            foreach (ResourceDictionary dictionary in Application.Current.Resources.MergedDictionaries)
            {
                dictionaryList.Add(dictionary);
            }
            string requestedCulture = "pack://application:,,,/UI;component/Language/" + lang + ".xaml";
            ResourceDictionary resourceDictionary = dictionaryList.FirstOrDefault(d => d.Source.OriginalString.Equals(requestedCulture));
            Application.Current.Resources.MergedDictionaries.Remove(resourceDictionary);
            Application.Current.Resources.MergedDictionaries.Add(resourceDictionary);
        }

每次設置語言就將語言放到最下方,這樣調用到的就是你所選擇的語言了。需要注意的是這裏用到的URI地址requestedCulture,前面基本不用變,UI是你的項目名稱,Language是你cn.xaml和en.xaml兩個資源詞典文件所在文件夾。

在WPF界面上的使用方式如下:

設置Content :Content="{StaticResource SelectSectionFile}"

靜態資源的名稱剛剛好。

在後臺的調用最好的方式是自己新定義一個全局變量來方便調用查詢,調用的方式如下:

if (Application.Current.Resources.MergedDictionaries.Count > 0) {
                ResourceDictionary RD = Application.Current.Resources.MergedDictionaries[Application.Current.Resources.MergedDictionaries.Count - 1];//更新完語言後再操作
                string pro= RD["Project"].ToString();//RD[""]冒號內對應XAML中的KEY
                int I=Convert.ToInt32(RD["I"].ToString());//如果調用不存在的KEY——I,會出錯,最好加個try-catch驗證一下。
            }

2020年5月26日19:00記——廣州

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