WPF 语言包
1定义语言资源
在Luanguage
文件夹下添加StringResource.en-US.xaml
与StringResource.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的ResourceDictionary
的MergedDictionaries
资源字典集合中。
<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";
}
}
}
5效果
积跬步以至千里:) (:一阵没来由的风