當前位置: 華文世界 > 科技

WPF多語言支持:簡單靈活的動態切換,讓你的程式支持多國語言

2024-02-13科技

概述: 本範例演示了在WPF應用程式中實作多語言支持的詳細步驟。透過資源字典和數據繫結,以及使用語言管理器類,應用程式能夠在執行時動態切換語言。這種方法使得多語言支持更加靈活,便於維護,同時提供清晰的程式碼結構。

在WPF中實作多語言的一種常見方法是使用資源字典和數據繫結。以下是一個詳細的步驟和範例原始碼,演示如何在WPF應用程式中實作動態切換語言。 文末提供程式碼下載。

先看效果:

步驟 1: 準備資原始檔

首先,為每種語言建立一個資原始檔。資原始檔的命名約定為 Resources.{語言程式碼}.xaml 。例如,Resources.en-US.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><!-- Resources.zh-Hans.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" >姓名</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: class="Sample_LanguageManager.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: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應用程式就能夠支持多語言,並且可以在執行時動態切換語言。這種方法具有靈活性,方便維護和管理多語言應用程式。

原始碼獲取:私我