WPF應用怎么實現在線切換語言功能,方法可以有好多種:
一、開發兩套界面,在點擊切換語言時調用相應的界面,最low
二、通過讀取事先做好的語言對照表,可以使excel文件、xml文件等,加載之后保存在全局字典或者datatable中都行,從而進行語言切換
三、利用WPF中的動態加載資源字典進行切換。
下面就第三種方法進行闡述:
第一步:使用資源字典,首先新建兩個字典文件en-us.xaml、zh-cn.xaml。定義中英文的字符串在這里面【注意:添加xmlns:s="clr-namespace:System;assembly=mscorlib】
zh-cn.xam如下:
<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> <sys:String x:Key="biName">產品名稱:</sys:String> <sys:String x:Key="biCopyRight">版權所有:</sys:String> <sys:String x:Key="biWeb">公司網址:</sys:String> <sys:String x:Key="biVersion">版本號:</sys:String> <sys:String x:Key="biSelectDevice">選擇設備</sys:String> <sys:String x:Key="biClose">關閉</sys:String> <sys:String x:Key="biSetting">設置</sys:String> <sys:String x:Key="biLanguage">語言</sys:String> </ResourceDictionary>
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> <sys:String x:Key="biName">Product Name:</sys:String> <sys:String x:Key="biCopyRight">CopyRight:</sys:String> <sys:String x:Key="biWeb">Web:</sys:String> <sys:String x:Key="biVersion">Version:</sys:String> <sys:String x:Key="biSelectDevice">Choose Device</sys:String> <sys:String x:Key="biClose">Close</sys:String> <sys:String x:Key="biSetting">Setting</sys:String> <sys:String x:Key="biLanguage">Language</sys:String> </ResourceDictionary>
第二步:講兩個資源字典添加到App.xaml中,這里注意下,因為兩個字典中有同樣字符,如果沒有動態更改,默認后添加的生效
App.xaml如下:
<Application x:Class="CANTest.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors" xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core" StartupUri="MainWindow.xaml" Startup="OnAppStartup_UpdateThemeName" Exit="Application_Exit"> <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>
第三步:在Properties.Settings中添加應用程序設置,保存上次關閉程序的語言環境
第四步:在App.Xaml.cs中添加必要的更新語言函數,動態切換,重新加載資源文件
using System; using System.Collections.Generic; using System.Configuration; using System.Data; using System.Linq; using System.Windows; using DevExpress.Xpf.Core; using System.Threading; using System.Globalization; namespace WPFApplication { /// <summary> /// Interaction logic for App.xaml /// </summary> public partial class App : Application { private void OnAppStartup_UpdateThemeName(object sender, StartupEventArgs e) { DevExpress.Xpf.Core.ApplicationThemeHelper.UpdateApplicationThemeName(); } public static string Language { get; set; } protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); GetLanguage(); } private void Application_Exit(object sender, ExitEventArgs e) { SaveLanguage(); //關閉所有線程,即關閉此進程 System.Environment.Exit(0); //MessageBoxManager.Unregister(); } #region Method /// <summary> /// 開機啟動默認的語言 /// </summary> private void GetLanguage() { Language = string.Empty; try { Language = CANTest.Properties.Settings.Default.Language.Trim(); } catch (Exception) { } Language = string.IsNullOrEmpty(Language) ? "en-US" : Language; //update Language UpdateLanguage(); } /// <summary> /// 保存語言設置 /// </summary> private void SaveLanguage() { try { CANTest.Properties.Settings.Default.Language = Language; CANTest.Properties.Settings.Default.Save(); } catch (Exception) { } } /// <summary> /// 更換語言包 /// </summary> public static void UpdateLanguage() { 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); } } #endregion } }
第五步:在界面設計器中需要顯示的位置添加動態資源
例如:
<Button x:Name="buttonNewTaskWindow" Content="{DynamicResource File}"/>
<Button x:Name="buttonProperty" Content="{DynamicResource Help}"/>
第六步:增加選擇語言命令
/// <summary> /// 語言選擇命令 /// </summary> private static RoutedUICommand chooseLanguage = new RoutedUICommand("ChooseLanguage", "ChooseLanguage", typeof(Commands)); public static RoutedUICommand ChooseLanguage { get { return chooseLanguage; } } #region 中英文切換 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"; } App.UpdateLanguage(); } #endregion