【已验证】通过资源实现WPF软件界面多语言化

时间:2022-07-25 15:53:08

运行效果为:

【已验证】通过资源实现WPF软件界面多语言化

【已验证】通过资源实现WPF软件界面多语言化

1、在项目中新建一个Lang文件夹用于保存我们的语言文件

2、 在Lang文件夹中,新建"ResourceDictionary(WPF)",命名为"en-US.xaml",并将其BuildAction设置为Page,这是一个默认语言资源文件,其将被编译(而不是松散链接,这样可以确保在软件语言包丢失或没有某国家或地区的对应语言包时可以有一种默认的界面语言):我们这里采用英文作为默认语言:

<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="Uid">UserName</sys:String>
    <sys:String x:Key="Pwd">Passord</sys:String>
    <sys:String x:Key="Lang">Language</sys:String>
    <sys:String x:Key="OK">OK </sys:String>
    <sys:String x:Key="Cancel">Cancel</sys:String>
</ResourceDictionary>

然后,我们添加另一国语言,比如中文,在Lang文件夹中,新建"ResourceDictionary(WPF)",命名为"zh-CN.xaml",并将其BuildAction设置为Content,将CopyToOutputDirctory设置为"if new",这样,我们的中文语言文件为将被拷贝到应用程序目录下的Lang目录下,其他的非默认语言的语言文件都应该采取这种方式.

<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="Uid">您的账号</sys:String>
    <sys:String x:Key="Pwd">您的密码</sys:String>
    <sys:String x:Key="Lang">您的语言</sys:String>
    <sys:String x:Key="OK"> 确定</sys:String>
    <sys:String x:Key="Cancel">取消</sys:String>   
</ResourceDictionary>

3、为了让编码人员在设计器(比如VS,Blend)中所见即所得地看到界面文本,我们应该将默认语言资源加入到应用程序的资源列表中。

<Application x:Class="WpfApplication27.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="lang\en-US.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
      

    </Application.Resources>

</Application>

4、在窗口中进行动态绑定

<Window x:Class="WpfApplication27.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="269" Width="294">
  
    <Grid>
        <Label Content="{DynamicResource Uid}" Height="28" HorizontalAlignment="Left" Margin="36,31,0,0" Name="label1" VerticalAlignment="Top" />
        <Label Content="{DynamicResource Pwd}" Height="28" HorizontalAlignment="Left" Margin="36,73,0,0" Name="label2" VerticalAlignment="Top" />
        <TextBox Height="23" HorizontalAlignment="Left" Margin="113,31,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" />
        <TextBox Height="23" HorizontalAlignment="Left" Margin="113,74,0,0" Name="textBox2" VerticalAlignment="Top" Width="120" />
        <Label Height="28" HorizontalAlignment="Left" Margin="36,112,0,0" Name="label3" VerticalAlignment="Top" Content="{DynamicResource Lang}" />
        <ComboBox Height="23" Margin="113,112,0,0" Name="comboBox1" VerticalAlignment="Top" SelectionChanged="comboBox1_SelectionChanged" Width="120" HorizontalAlignment="Left" IsSynchronizedWithCurrentItem="{x:Null}">
            <ComboBoxItem Content="中文" />
            <ComboBoxItem Content="English" />
        </ComboBox>
        <Button Content="{DynamicResource OK}" Height="23" HorizontalAlignment="Left" Margin="49,175,0,0" Name="button1" VerticalAlignment="Top" Width="75" />
        <Button Content="{DynamicResource Cancel}" Height="23" HorizontalAlignment="Left" Margin="150,175,0,0" Name="button2" VerticalAlignment="Top" Width="75" />
    </Grid>
</Window>

5、后台代码

App.xaml.cs:

public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            LoadLanguage();
        }

        private void LoadLanguage( )
        {
            CultureInfo currentCultureInfo = CultureInfo.CurrentCulture;

            ResourceDictionary langRd = null;

            try
            {
                langRd =
                    Application.LoadComponent(
                             new Uri(@"Lang\" + currentCultureInfo.Name + ".xaml", UriKind.Relative))
                    as ResourceDictionary;
            }
            catch
            {
            }

            if (langRd != null)
            {
                if (this.Resources.MergedDictionaries.Count > 0)
                {
                    this.Resources.MergedDictionaries.Clear();
                }
                this.Resources.MergedDictionaries.Add(langRd);
            }


        }

    }

MainWindow.xaml.cs:

private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ResourceDictionary langRd = null;

            string lang = "";

            ComboBoxItem cbi = (ComboBoxItem)comboBox1.SelectedItem;

            string sitem = cbi.Content.ToString();


            if (sitem == "中文")
                lang = "zh-CN";
            if (sitem == "English")
                lang = "en-US";

            try
            {
                langRd =
                    Application.LoadComponent(
                             new Uri(@"Lang\" + lang + ".xaml", UriKind.Relative))
                    as ResourceDictionary;
            }
            catch
            {
            }

            if (langRd != null)
            {
                if (this.Resources.MergedDictionaries.Count > 0)
                {
                    this.Resources.MergedDictionaries.Clear();
                }
                this.Resources.MergedDictionaries.Add(langRd);
            }
        }

下载demo:http://115.com/file/dp131516