WPF 多语言

来源:转载

在我们程序中 有时候会涉及到多个语言、那么在WPF中可以使用 [资源字典] Resources.xaml 或者是Resources.resx 资源文件

本人认为呢 使用资源字典比较方便些。。

资源文件:

 1 <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

2 xmlns:sys="clr-namespace:System;assembly=mscorlib"

3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

4 <sys:String x:Key="Window1Title">Window</sys:String>

5 <sys:String x:Key="UserLog">UserLogin</sys:String>

6 <sys:String x:Key="UserCode">UserCode</sys:String>

7 <sys:String x:Key="UserName">UserPwd</sys:String>

8 <sys:String x:Key="BtnLoginOk">Login</sys:String>

9 <sys:String x:Key="BtnLoginCancel">Cancel</sys:String>

10 </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="Window1Title">Window窗前</sys:String>

<sys:String x:Key="UserLog">用户登陆</sys:String>

<sys:String x:Key="UserCode">用户名称</sys:String>

<sys:String x:Key="UserName">用户密码</sys:String>

<sys:String x:Key="BtnLoginOk">登录</sys:String>

<sys:String x:Key="BtnLoginCancel">取消</sys:String>

</ResourceDictionary>

Windows窗体:

窗体 View

<Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="{DynamicResource Window1Title}" Height="287" Width="406">

<Grid>

<GroupBox Header="{DynamicResource UserLog}">

<Grid>

<Grid.RowDefinitions>

<RowDefinition Height="45*"/>

<RowDefinition Height="48*"/>

<RowDefinition Height="58*"/>

<RowDefinition Height="100*"/>

</Grid.RowDefinitions>

<TextBlock HorizontalAlignment="Left" Margin="36,22,0,0" Grid.Row="1" TextWrapping="Wrap" Text="{DynamicResource UserCode}" VerticalAlignment="Top"/>

<TextBlock HorizontalAlignment="Left" Margin="36,23,0,0" Grid.Row="2" TextWrapping="Wrap" Text="{DynamicResource UserName}" VerticalAlignment="Top"/>

<TextBox HorizontalAlignment="Left" Height="23" Margin="125,22,0,0" Grid.Row="1" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>

<TextBox HorizontalAlignment="Left" Height="23" Margin="125,23,0,0" Grid.Row="2" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>

<Button Content="{DynamicResource BtnLoginOk}" HorizontalAlignment="Left" Margin="78,29,0,0" Grid.Row="3" VerticalAlignment="Top" Width="75"/>

<Button Content="{DynamicResource BtnLoginCancel}" HorizontalAlignment="Left" Margin="192,29,0,0" Grid.Row="3" VerticalAlignment="Top" Width="75"/>

<Button Content="en-US" Name="btnenUS" HorizontalAlignment="Left" Margin="301,10,0,0" VerticalAlignment="Top" Width="75" Click="btnenUS_Click"/>

<Button Content="zh-CN" Name="btnzhCN" HorizontalAlignment="Left" Margin="221,10,0,0" VerticalAlignment="Top" Width="75" Click="btnzhCN_Click"/>

</Grid>

</GroupBox>

</Grid>

</Window>



窗体 CS

public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void btnzhCN_Click(object sender, RoutedEventArgs e) { App.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("Resources/Resources.zh-CN.xaml", UriKind.RelativeOrAbsolute) }); } private void btnenUS_Click(object sender, RoutedEventArgs e) { App.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("Resources/Resources.en-US.xaml", UriKind.RelativeOrAbsolute) }); } }



/// <summary>

/// App.xaml 的交互逻辑 /// </summary>

public partial class App : Application { public App() { this.Startup += App_Startup; } void App_Startup(object sender, StartupEventArgs e) { //第一种方式就是用配置文件 配置 //string appLang = ConfigurationManager.AppSettings.Get("Languages"); //App.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() //{ Source = new Uri(appLang, UriKind.RelativeOrAbsolute) }); //第二种方式 就是获取当前系统的语言

string lang=System.Globalization.CultureInfo.InstalledUICulture.Name.ToString(CultureInfo.InvariantCulture); switch (lang) { case "zh-CN": App.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("Resources/Resources.zh-CN.xaml", UriKind.RelativeOrAbsolute) }); break; case "en-US": App.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("Resources/Resources.en-US.xaml", UriKind.RelativeOrAbsolute) }); break; default: App.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("Resources/Resources.en-US.xaml", UriKind.RelativeOrAbsolute) }); break; } } }


app.config 配置文件

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

<appSettings>

<add key="Languages" value="Resources/Resources.en-US.xaml"/>

</appSettings>

</configuration>


这样就基本完成了。。呵呵 


分享给朋友:
您可能感兴趣的文章:
随机阅读: