1. 在WPF中国际化使用的是 .xaml文件的格式

      如图:Resource Dictionary (WPF)

      WPF实现无刷新动态切换多语言(国际化)

2. 创建默认的语言文件和其他语言文件

       这里以英语为默认语言,新建一个 Resource Dictionary (WPF)文件,并命名为DefaultLanguage.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="OK">
        OK
    </sys:String>
    <sys:String x:Key="Cancel">
        Cancel
    </sys:String>
</ResourceDictionary>

   默认语言文件的 BuildAction要设置为 Page,如图:

       WPF实现无刷新动态切换多语言(国际化)

       为了便于管理,一般将所有的语言文件都放在一个目录下,这里创建lang目录,

       然后在创建另一个语言文件,这里是中文,命名为 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="OK">
        确定
    </sys:String>

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

       其他非默认语言的设置应该如下:

       BuildAction设置为:Content ;CopyToOutputDirectory设置为:Copy if newer (先这样做吧,原因未清)

        WPF实现无刷新动态切换多语言(国际化)

3.在App.xaml中配置默认语言:

<Application x:Class="LanTest.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\DefaultLanguage.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

4.实际使用(敲代码了) 

     4.1. 界面效果如下:

           WPF实现无刷新动态切换多语言(国际化)

    4.2. 界面的.xaml代码

 1 <Window x:Class="LanTest.MainWindow"
 2         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 3         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 4         Title="MainWindow" Height="350" Width="525">
 5     <Grid>
 6         <!--这里的{DynamicResource OK}就是动态调用 资源中的key为OK的内容-->
 7         <Button Content="{DynamicResource OK}" HorizontalAlignment="Left" Margin="134,161,0,0" VerticalAlignment="Top" Width="104" Height="38"/>
 8         <Button Content="{DynamicResource Cancel}" HorizontalAlignment="Left" Margin="278,161,0,0" VerticalAlignment="Top" Width="100" Height="38"/>
 9         <Button Content="Button" HorizontalAlignment="Left" Margin="287,59,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click" Loaded="Button_Loaded"/>
10         <ComboBox Name="cbLang" HorizontalAlignment="Left" Margin="118,59,0,0" VerticalAlignment="Top" Width="120">
11         </ComboBox>
12 
13     </Grid>
14 </Window>
View Code

相关文章:

  • 2021-06-24
  • 2021-09-28
  • 2021-10-21
  • 2022-12-23
  • 2022-12-23
  • 2021-11-25
猜你喜欢
  • 2021-05-06
  • 2022-12-23
  • 2022-12-23
  • 2021-09-29
  • 2021-08-16
  • 2021-12-01
  • 2022-12-23
相关资源
相似解决方案