例英語のスイッチ:
静的スイッチ(あなたがアプリケーションを切り替えるたびに再起動する必要があります)
1.新しいWPF APP(.NET Frameworkの)プロジェクト、StaticLanguageSelectを作成します。
2.プロジェクト名を右クリックし、Resources.en-us.resxという名前の新しい項目を選択リソースファイルの種類を追加、ドラッグアンド示すように、プロパティにファイルをドロップします。
3.必要Resources.resx中国語に格納された名前と値を使用することは、英語のように表さResources.en-us.resx店舗で必要言いました:
注意:アクセス修飾子の中国のリソースファイルResources.resxをパブリックに変更します
プロパティの下4. Settings.settings、現在のスレッドの文化を格納するための1つを作成します。(バックは、システム環境に適応するために、初めて開始するように設定されているため、初期値の値は、空です。)
CultureInfoクラスで使用されるコードの後に、.NET Frameworkは、複数の言語と同じデータを異なる地域や文化のユーザーを満たすために、異なる地域や文化に適応することを可能にするクラス環境の習慣のためのサポートが付属しています。
MainWindows.xamlとMainWindows.xaml.csの次のコード書き込み処理。
メイン関数の形で書かれた.NET Frameworkの4.5.2バージョンは、唯一の作品については、注意:APP.xaml.csで高.NET Frameworkのバージョン4.6.2がで書き込もうとする必要があります。
1 <Window x:Class="StaticLanguageSelect.MainWindow"
2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
5 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
6 xmlns:p="clr-namespace:StaticLanguageSelect.Properties"
7 xmlns:local="clr-namespace:StaticLanguageSelect"
8 mc:Ignorable="d"
9 Title="MainWindow" Height="450" Width="800">
10 <Grid>
11 <StackPanel>
12 <TextBlock Text="{x:Static p:Resources.String1}"/>
13 <Button Content="{x:Static p:Resources.SelectLanguage}" Click="Button_Click"/>
14 <Button Content="{x:Static p:Resources.Show}" Click="Button2_Click"/>
15 </StackPanel>
16 </Grid>
17 </Window>
1 using StaticLanguageSelect.Properties;
2 using System.Diagnostics;
3 using System.Globalization;
4 using System.Resources;
5 using System.Threading;
6 using System.Threading.Tasks;
7 using System.Windows;
8
9 namespace StaticLanguageSelect
10 {
11 /// <summary>
12 /// Interaction logic for MainWindow.xaml
13 /// </summary>
14 public partial class MainWindow : Window
15 {
16 private string _name;
17 private ResourceManager _currentResource;
18
19 public MainWindow()
20 {
21 var cultureName = Settings.Default.CultureName;
22
23 //如果Settings中的CultureName不为空,就实例化该种CultureInfo实例,使用它。
24 if (!string.IsNullOrEmpty(cultureName))
25 {
26 try
27 {
28 var cultureInfo = new CultureInfo(cultureName);
29 CultureInfo.DefaultThreadCurrentCulture = cultureInfo;
30 CultureInfo.DefaultThreadCurrentUICulture = cultureInfo;
31 }
32 catch (CultureNotFoundException)
33 {
34 throw;
35 }
36 }
37
38 //获取到当前线程的Culture,赋值到Settings设置中,并保存。
39 Settings.Default.CultureName = Thread.CurrentThread.CurrentUICulture.Name;
40 Settings.Default.Save();
41
42 //实例化ResourceManager,来获得当前的Resource配置。
43 _currentResource = new ResourceManager("StaticLanguageSelect.Properties.Resources", typeof(Resources).Assembly);
44
45 InitializeComponent();
46 }
47
48 private void Button_Click(object sender, RoutedEventArgs e)
49 {
50 Settings.Default.CultureName = Thread.CurrentThread.CurrentUICulture.Name == "en-US" ? "zh-CN" : "en-US";
51 Settings.Default.Save();
52
53 Task.Delay(500);
54
55 //重启WPF程序
56 Process.Start(Application.ResourceAssembly.Location);
57 Application.Current.Shutdown();
58 }
59
60 private void Button2_Click(object sender, RoutedEventArgs e)
61 {
62 _name = _currentResource.GetString("Name");
63 MessageBox.Show(_name);
64 }
65 }
66 }