WPF國際化&語言切換

WPF國際化建議使用官方推薦方式,當然也可以通過自己寫個類實現,沒必要重複造輪子,如下記錄過程

添加資源文件

  • 新建文件,並按照語言命名

  • 適當修改生成文件,添加字符lib
    <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">
        
    </ResourceDictionary>

     

  • 開始添加相對應的語言翻譯等
    <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="helpImage">Help</sys:String>
    </ResourceDictionary>

     

  • 修改文件生成

引用定義的資源

  • 首先需要定義默認的資源文件,此處先添加默認資源文件,後面詳細說如何引用切換資源文件,在APP.xaml中添加引用資源文件
<Application x:Class="ServoStudio.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:ServoStudio"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="lang/EN.xaml"></ResourceDictionary>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
  • xmal中引用資源,使用DynamicResource,後面加上對應的key即可
<Image Name="helpImage" Source="png/description.png" Cursor="Help" ToolTip="{DynamicResource helpImage}"></Image>
  • 後臺中引用,下面介紹兩種方式

在屬性中設置,設置label的content

label.SetResourceReference(ContentProperty, "Graph1Lable1");

直接設置,設置label的content

label.Content = App.Current.Resources.MergedDictionaries[0]["updateTrueDButton1"] as string

兩種方式的區別這裏沒有詳細比較,可能和切換語言後是否直接可以刷新有關,第一種可以直接刷新,第二種不可以,這裏沒有做驗證,在我的應用中需要加載其他配置文件,所以需要重啓後再生效

經測試可不使用MergedDictionaries直接使用

label.Content = App.Current.Resources["updateTrueDButton1"] as string

切換資源文件,即語言修改

前面已經設置了默認語言,但是我們應該包根據設置來選擇相關語言,所以需要在後臺選擇設置資源文件

此處給出我的方式,先清除之前的配置,然後加載進來新的,可根據這個demo修改成選擇後調用此方法

private void InitLang()
        {
            //總體設置
            App.Current.Resources.MergedDictionaries.Clear();
            ResourceDictionary resourceDictionary = new ResourceDictionary();
            switch (Properties.Settings.Default.lang)
            {
                case "zh-CN":
                    resourceDictionary.Source = new Uri("lang/zh-CN.xaml", UriKind.Relative);
                    break;
                default:
                    resourceDictionary.Source = new Uri("lang/zh-CN.xaml", UriKind.Relative);
                    break;
            }
            App.Current.Resources.MergedDictionaries.Add(resourceDictionary);
}

一些問題

實際使用過程中發現的幾個問題

  • 還有需要注意的是這裏使用的方式是直接調用了MergedDictionaries[0],所以需要確保該引用值和裏面的對應
  • 加載的文件注意是添加到App總的資源中,而不是當前頁面的資源

 

隨着認知的增長,今天記錄東西到了明天可能我就認爲他是錯的

聞道有先後。。

from:https://blog.csdn.net/shaynerain

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