合肥思拓空间 发表于 2024-5-1 09:10:09

WPF多语言支持:简单灵活的动态切换,让你的程序支持多国语言

 
概述:本示例演示了在WPF应用程序中实现多语言支持的详细步骤。通过资源字典和数据绑定,以及使用语言管理器类,应用程序能够在运行时动态切换语言。这种方法使得多语言支持更加灵活,便于维护,同时提供清晰的代码结构。
在WPF中实现多语言的一种常见方法是使用资源字典和数据绑定。以下是一个详细的步骤和示例源代码,演示如何在WPF应用程序中实现动态切换语言。文末提供代码下载。
先看效果:
 
步骤 1: 准备资源文件
首先,为每种语言创建一个资源文件。资源文件的命名约定为Resources.{语言代码}.xaml。例如,Resources.en-US.xaml表示英语(美国)的资源文件。
在每个资源文件中,添加键值对(本例的前缀为窗体名称,是为了避免不同窗体有相同命名的问题),表示不同控件或文本的本地化字符串。例如:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:system="clr-namespace:System;assembly=netstandard">
    <system:String x:Key="MainWindow_name">Name</system:String>
    <system:String x:Key="MainWindow_age">Age</system:String>
    <system:String x:Key="MainWindow_Language">简体中文</system:String>
</ResourceDictionary>


<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:system="clr-namespace:System;assembly=netstandard">
    <system:String x:Key="MainWindow_name" >姓名</system:String>
    <system:String x:Key="MainWindow_age">年龄</system:String>
    <system:String x:Key="MainWindow_Language">English</system:String>
</ResourceDictionary>步骤 2: 创建语言管理器类
创建一个语言管理器类,用于切换当前应用程序的语言。这个类可能包含一个属性,表示当前的CultureInfo,以及一个方法来切换语言。
using System;
using System.Globalization;
using System.Windows;

public static class LanguageManager
{
    private static ResourceDictionary currentLanguage;

    public static ResourceDictionary CurrentLanguage
    {
      get { return currentLanguage; }
      set
      {
            if (currentLanguage != value)
            {
                currentLanguage = value;
                UpdateLanguage();
            }
      }
    }

    private static void UpdateLanguage()
    {
      if (Application.Current.Resources.MergedDictionaries.Contains(currentLanguage))
      {
            Application.Current.Resources.MergedDictionaries.Remove(currentLanguage);
            Application.Current.Resources.MergedDictionaries.Add(currentLanguage);
      }
      else
      {
            Application.Current.Resources.MergedDictionaries.Add(currentLanguage);
      }
    }
}步骤 3: 在WPF应用程序中使用资源字典和数据绑定
在XAML文件中,使用Binding来绑定控件的内容或文本到资源字典中的相应键。例如:
<Window x:
      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:Sample_LanguageManager"
      mc:Ignorable="d"
      Title="MainWindow" Height="450" Width="800">
    <Grid>
      <Grid.Background>
            <LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
                <LinearGradientBrush.GradientStops>
                  <GradientStop Color="#FF9DC5FD" Offset="0" />
                  <GradientStop Color="#FF4242CF" Offset="1" />
                </LinearGradientBrush.GradientStops>
            </LinearGradientBrush>
      </Grid.Background>
      <Grid.RowDefinitions>
            <RowDefinition Height="90"/>
            <RowDefinition Height="60"/>
            <RowDefinition Height="60"/>
      </Grid.RowDefinitions>
      <Label Grid.Row="0"Content="{DynamicResource MainWindow_name}" d:Content="姓名" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="36" Foreground="White" />
      <Label Grid.Row="1" Content="{DynamicResource MainWindow_age}" d:Content="年龄" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="36" Foreground="White" />
      <Button Grid.Row="2" Content="{DynamicResource MainWindow_Language}" d:Content="切换语言" Click="SwitchToFrench_Click" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="22"/>
    </Grid>
</Window>步骤 4: 在应用程序启动时设置语言
在应用程序启动时,设置LanguageManager的CurrentLanguage属性以选择初始语言。这可以在App.xaml.cs中的OnStartup方法中完成。
public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
      // 设置初始语言,例如英语
      LanguageManager.CurrentLanguage = new ResourceDictionary { Source = new Uri("Resources.en-US.xaml", UriKind.Relative) };

      // 其他启动逻辑...

      base.OnStartup(e);
    }
}步骤 5: 实现语言切换
你可以在应用程序中的某个地方提供用户切换语言的选项。在语言切换事件中,更新LanguageManager的CurrentLanguage属性(因为是个简单的例子,所以只提供中英文切换,实际可提供更多语言字典来切换)。
      private void SwitchToFrench_Click(object sender, RoutedEventArgs e)
      {
            if (LanguageManager.CurrentLanguage.Source.OriginalString.Contains("en-US"))
            {
                LanguageManager.CurrentLanguage = new ResourceDictionary { Source = new Uri("Resources.zh-Hans.xaml", UriKind.Relative) };
            }
            else
            {
                LanguageManager.CurrentLanguage = new ResourceDictionary { Source = new Uri("Resources.en-US.xaml", UriKind.Relative) };
            }
      }通过以上步骤,你的WPF应用程序就能够支持多语言,并且可以在运行时动态切换语言。这种方法具有灵活性,方便维护和管理多语言应用程序。
源代码获取:https://pan.baidu.com/s/1JBbd6F7vHMZ4bIL8nhzBEA?pwd=6666 
 



来源:https://www.cnblogs.com/hanbing81868164/p/18166976
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!
页: [1]
查看完整版本: WPF多语言支持:简单灵活的动态切换,让你的程序支持多国语言