<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      WPF實現本地化多語言的幾種方式

      話不多說,我們直接上源碼開干。

      1.第一種方式: 使用字典Dictionary.xaml

      搭建系統框架,使用MVVM

      頁面布局方式如下:

      <Window x:Class="WPFDemoMVVM.View.LanguageChangeView"
      		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:WPFDemoMVVM.View"
      		xmlns:lex="http://wpflocalizeextension.codeplex.com"
      		xmlns:lang="clr-namespace:WPFDemoMVVM.Resources"
      		mc:Ignorable="d"
      		Title="{DynamicResource Home}" Height="300" Width="600">
      	<Grid>
      		<Grid.RowDefinitions>
      			<RowDefinition/>
      			<RowDefinition/>
      		</Grid.RowDefinitions>
      		<StackPanel Grid.Row="0" VerticalAlignment="Center" HorizontalAlignment="Center" >
      			<TextBlock Text="{DynamicResource Welcome}" FontSize="48"/>
      
      		</StackPanel>
      		<StackPanel Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
      			<Button x:Name="btnEnglish" Tag="en" Content="{DynamicResource English}" Margin="10" Command="{Binding ChangeLanguageCommand}" CommandParameter="{Binding Tag, RelativeSource={RelativeSource Self}}" Width="100" Height="30"/>
      			<Button x:Name="btnLanguage" Tag="zh-CN" Content="{DynamicResource Chinese}" Margin="10" Command="{Binding ChangeLanguageCommand}" CommandParameter="{Binding Tag, RelativeSource={RelativeSource Self}}" Width="100" Height="30"/>
      			<Button Content="{DynamicResource ShowMessage}" Margin="20" Command="{Binding ShowMessageCommand}" Width="100" Height="30"></Button>
      		</StackPanel>
      	</Grid>
      </Window>
      

      后端ViewModel:

      using CommunityToolkit.Mvvm.ComponentModel;
      using CommunityToolkit.Mvvm.Input;
      using System;
      using System.Collections.Generic;
      using System.Globalization;
      using System.Linq;
      using System.Text;
      using System.Threading;
      using System.Threading.Tasks;
      using System.Windows;
      using WPFDemoMVVM.Resources;
      using WPFLocalizeExtension.Engine;
      
      namespace WPFDemoMVVM.ViewModel
      {
      	public partial class LanguageChangeViewModel: ObservableObject
      	{
      
      		[RelayCommand]
      		private void ChangeLanguage(string languageCode)
      		{
      			if (string.IsNullOrWhiteSpace(languageCode))
      				return;
      			// 切換語言資源
      			var dict = new ResourceDictionary();
      			switch (languageCode)
      			{
      				case "en":
      					dict.Source = new Uri("Language/en_Dictionary.xaml", UriKind.Relative);
      					break;
      				case "zh-CN":
      					dict.Source = new Uri("Language/zh_Dictionary.xaml", UriKind.Relative);
      					break;
      				default:
      					return;
      			}
      			// 替換 Application 的 Resource
      			Application.Current.Resources.MergedDictionaries.Clear();
      			Application.Current.Resources.MergedDictionaries.Add(dict);
      		}
      
      		[RelayCommand]
      		private void ShowMessage()
      		{
      			MessageBox.Show(Application.Current.TryFindResource("Hello").ToString(), Application.Current.TryFindResource("Prompt").ToString(), MessageBoxButton.OK, MessageBoxImage.Information);
      		}
      	}
      }
      

      新建兩個資源文件:en_Dictionary.xaml和zh_Dictionary.xaml

      en_Dictionary.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=netstandard">
      	<sys:String x:Key="Home">Home</sys:String>
      	<sys:String x:Key="MenuMsg">Menu Management</sys:String>
      	<sys:String x:Key="Welcome">Welcome to you</sys:String>
      	<sys:String x:Key="English">English</sys:String>
      	<sys:String x:Key="Chinese">Chinese</sys:String>
      	<sys:String x:Key="ShowMessage">Show Message</sys:String>
      	<sys:String x:Key="Prompt">Prompt</sys:String>
      	<sys:String x:Key="Hello">Hello</sys:String>
      </ResourceDictionary>
      

      zh_Dictionary.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=netstandard">
      	<sys:String x:Key="Home">首頁</sys:String>
      	<sys:String x:Key="MenuMsg">菜單管理</sys:String>
      	<sys:String x:Key="Welcome">歡迎你</sys:String>
      	<sys:String x:Key="English">英文</sys:String>
      	<sys:String x:Key="Chinese">中文</sys:String>
      	<sys:String x:Key="ShowMessage">顯示信息</sys:String>
      	<sys:String x:Key="Prompt">提示</sys:String>
      	<sys:String x:Key="Hello">你好</sys:String>
      </ResourceDictionary>
      

      在App.xaml中將資源合并進入項目:

      <Application x:Class="WPFDemoMVVM.App"
      			 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      			 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      			 xmlns:local="clr-namespace:WPFDemoMVVM"
      			 Startup="Application_Startup">
      	<Application.Resources>
      		<ResourceDictionary>
      			<ResourceDictionary.MergedDictionaries>
      				<ResourceDictionary Source="pack://application:,,,/WPFDemoMVVM;component/Language/zh_Dictionary.xaml"/>
      				<ResourceDictionary Source="pack://application:,,,/HandyControl;component/Themes/SkinDefault.xaml"/>
      				<ResourceDictionary Source="pack://application:,,,/HandyControl;component/Themes/Theme.xaml"/>
      			</ResourceDictionary.MergedDictionaries>
      		</ResourceDictionary>
      	</Application.Resources>
      </Application>
      

      App進程里進行初始化配置:

      image

      效果如下圖:
      image

      image

      2. 第二種方式,使用resx資源文件,實現多語言本地化

      新建資源文件Lang.resx和Lang.zh-CN.resx
      image

      新建LanguageChangeView頁面布局:

      <Window x:Class="WPFDemoMVVM.View.LanguageChangeView"
      		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:WPFDemoMVVM.View"
      		xmlns:lex="http://wpflocalizeextension.codeplex.com"
      		xmlns:lang="clr-namespace:WPFDemoMVVM.Resources"
      		mc:Ignorable="d"
      		Title="{x:Static lang:Lang.Home}" Height="300" Width="600">
      	<Grid>
      		<Grid.RowDefinitions>
      			<RowDefinition/>
      			<RowDefinition/>
      		</Grid.RowDefinitions>
      		<StackPanel Grid.Row="0" VerticalAlignment="Center" HorizontalAlignment="Center" >
      			<TextBlock Text="{x:Static lang:Lang.Welcome}" FontSize="48"/>
      
      		</StackPanel>
      		<StackPanel Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
      			<Button x:Name="btnEnglish" Tag="en" Content="{x:Static lang:Lang.English}" Margin="10" Command="{Binding ChangeLanguageCommand}" CommandParameter="{Binding Tag, RelativeSource={RelativeSource Self}}" Width="100" Height="30"/>
      			<Button x:Name="btnLanguage" Tag="zh-CN" Content="{x:Static lang:Lang.Chinese}" Margin="10" Command="{Binding ChangeLanguageCommand}" CommandParameter="{Binding Tag, RelativeSource={RelativeSource Self}}" Width="100" Height="30"/>
      			<Button Content="{x:Static lang:Lang.ShowMessage}" Margin="20" Command="{Binding ShowMessageCommand}" Width="100" Height="30"></Button>
      		</StackPanel>
      	</Grid>
      </Window>
      

      ViewModel頁如下:

      using CommunityToolkit.Mvvm.ComponentModel;
      using CommunityToolkit.Mvvm.Input;
      using System;
      using System.Collections.Generic;
      using System.Globalization;
      using System.Linq;
      using System.Text;
      using System.Threading;
      using System.Threading.Tasks;
      using System.Windows;
      using WPFDemoMVVM.Resources;
      using WPFLocalizeExtension.Engine;
      
      namespace WPFDemoMVVM.ViewModel
      {
      	public partial class LanguageChangeViewModel: ObservableObject
      	{
      
      		[RelayCommand]
      		private void ChangeLanguage(string languageCode)
      		{
      			//第二種方式切換語言資源
      			var culture = new CultureInfo(languageCode);
      			Thread.CurrentThread.CurrentCulture = culture;
      			Thread.CurrentThread.CurrentUICulture = culture;
      		}
      
      
      		[RelayCommand]
      		private void ShowMessage()
      		{
      			MessageBox.Show(Lang.Hello,Lang.Prompt, MessageBoxButton.OK, MessageBoxImage.Information);
      		}
      	}
      }
      

      3.第三種方式,使用第三方庫:wpflocalizeextension,可以實現熱重載【推薦使用】

      引用三方庫:wpflocalizeextension

      視圖頁面LanguageChangeView 如下:
      導入命名空間:xmlns:lex="http://wpflocalizeextension.codeplex.com"
      使用綁定方式:Text="{lex:Loc WPFDemoMVVM:Lang:Welcome}"

      <Window x:Class="WPFDemoMVVM.View.LanguageChangeView"
      		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:WPFDemoMVVM.View"
      		xmlns:lex="http://wpflocalizeextension.codeplex.com"
      		xmlns:lang="clr-namespace:WPFDemoMVVM.Resources"
      		mc:Ignorable="d"
      		Title="{lex:Loc WPFDemoMVVM:Lang:Home}" Height="300" Width="600">
      	<Grid>
      		<Grid.RowDefinitions>
      			<RowDefinition/>
      			<RowDefinition/>
      		</Grid.RowDefinitions>
      		<StackPanel Grid.Row="0" VerticalAlignment="Center" HorizontalAlignment="Center" >
      			<TextBlock Text="{lex:Loc WPFDemoMVVM:Lang:Welcome}" FontSize="48"/>
      		</StackPanel>
      		<StackPanel Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
      			<Button x:Name="btnEnglish" Tag="en" Content="{lex:Loc WPFDemoMVVM:Lang:English}" Margin="10" Command="{Binding ChangeLanguageCommand}" CommandParameter="{Binding Tag, RelativeSource={RelativeSource Self}}" Width="100" Height="30"/>
      			<Button x:Name="btnLanguage" Tag="zh-CN" Content="{lex:Loc WPFDemoMVVM:Lang:Chinese}" Margin="10" Command="{Binding ChangeLanguageCommand}" CommandParameter="{Binding Tag, RelativeSource={RelativeSource Self}}" Width="100" Height="30"/>
      			<Button Content="{lex:Loc WPFDemoMVVM:Lang:ShowMessage}" Margin="20" Command="{Binding ShowMessageCommand}" Width="150" Height="30"></Button>
      		</StackPanel>
      	</Grid>
      </Window>
      

      或者也可以使用如下的方式,如果整個頁面都是引用同一個命名空間下的資源文件:
      需要設置
      lex:ResxLocalizationProvider.DefaultAssembly="WPFDemoMVVM";
      lex:ResxLocalizationProvider.DefaultDictionary="Lang";

      如果需要在設計的時候顯示當前的語言的文本可以設置:
      lex:LocalizeDictionary.DesignCulture="zh-CN"

      <Window x:Class="WPFDemoMVVM.View.LanguageChangeView"
      		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:WPFDemoMVVM.View"
      		xmlns:lex="http://wpflocalizeextension.codeplex.com"
      		xmlns:lang="clr-namespace:WPFDemoMVVM.Resources"
      		lex:ResxLocalizationProvider.DefaultAssembly="WPFDemoMVVM"
      		lex:ResxLocalizationProvider.DefaultDictionary="Lang"
      		lex:LocalizeDictionary.DesignCulture="zh-CN"
      		mc:Ignorable="d"
      		Title="{lex:Loc Home}" Height="300" Width="600">
      	<Grid>
      		<Grid.RowDefinitions>
      			<RowDefinition/>
      			<RowDefinition/>
      		</Grid.RowDefinitions>
      		<StackPanel Grid.Row="0" VerticalAlignment="Center" HorizontalAlignment="Center" >
      			<TextBlock Text="{lex:Loc Welcome}" FontSize="48"/>
      		</StackPanel>
      		<StackPanel Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
      	 <!--<Button x:Name="btnEnglish" Tag="en" Content="{lex:Loc English}" Margin="10" Command="{Binding ChangeLanguageCommand}" CommandParameter="{Binding Tag, RelativeSource={RelativeSource Self}}" Width="100" Height="30"/>
      	 <Button x:Name="btnLanguage" Tag="zh-CN" Content="{lex:Loc Chinese}" Margin="10" Command="{Binding ChangeLanguageCommand}" CommandParameter="{Binding Tag, RelativeSource={RelativeSource Self}}" Width="100" Height="30"/>-->
      	 <Button x:Name="btnEnglish" Tag="en" Content="{lex:Loc English}" Margin="10" Command="{Binding Source={x:Static lex:LocalizeDictionary.Instance},Path=SetCultureCommand}" CommandParameter="en" Width="100" Height="30"/>
      	 <Button x:Name="btnLanguage" Tag="zh-CN" Content="{lex:Loc Chinese}" Margin="10" Command="{Binding Source={x:Static lex:LocalizeDictionary.Instance},Path=SetCultureCommand}" CommandParameter="zh-CN" Width="100" Height="30"/>
      			<Button Content="{lex:Loc ShowMessage}" Margin="20" Command="{Binding ShowMessageCommand}" Width="150" Height="30"></Button>
      		</StackPanel>
      	</Grid>
      </Window>
      

      viewModel實現類如下:

      using CommunityToolkit.Mvvm.ComponentModel;
      using CommunityToolkit.Mvvm.Input;
      using System;
      using System.Collections.Generic;
      using System.Globalization;
      using System.Linq;
      using System.Text;
      using System.Threading;
      using System.Threading.Tasks;
      using System.Windows;
      using WPFDemoMVVM.Resources;
      using WPFLocalizeExtension.Engine;
      
      namespace WPFDemoMVVM.ViewModel
      {
      	public partial class LanguageChangeViewModel: ObservableObject
      	{
      		[RelayCommand]
      		private void ChangeLanguage(string languageCode)
      		{
      			////第三種方式切換語言資源
      			var culture = new CultureInfo(languageCode);
      			Thread.CurrentThread.CurrentCulture = culture;
      			Thread.CurrentThread.CurrentUICulture = culture;
      			LocalizeDictionary.Instance.Culture = culture;
      		}
      
      		[RelayCommand]
      		private void ShowMessage()
      		{
      			MessageBox.Show(Lang.Hello,Lang.Prompt, MessageBoxButton.OK, MessageBoxImage.Information);
      		}
      	}
      }
      

      效果如下:
      英文界面:
      image

      中文界面:
      image

      3.第四種方式,使用第三方庫:Antelcat.I18N.WPF,

      引入第三方庫:Antelcat.I18N.WPF,

      然后新建LangKeys類,包裝資源文件Lang:

      using Antelcat.I18N.Attributes;
      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      using System.Threading.Tasks;
      
      namespace LanguageLibrary.Resources
      {
      	[ResourceKeysOf(typeof(Lang))]
      	public partial class LangKeys
      	{
      	}    
      }
      

      視圖頁面如下:

      <Window x:Class="WPFDemoMVVM.View.LanguageChangeView"
      		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:WPFDemoMVVM.View"
      		xmlns:lang="clr-namespace:WPFDemoMVVM.Resources"
      		mc:Ignorable="d"
      		Title="{I18N {x:Static lang:LangKeys.Home}}" Height="300" Width="600">
      	<Grid>
      		<Grid.RowDefinitions>
      			<RowDefinition/>
      			<RowDefinition/>
      		</Grid.RowDefinitions>
      		<StackPanel Grid.Row="0" VerticalAlignment="Center" HorizontalAlignment="Center" >
      			<TextBlock Text="{I18N {x:Static lang:LangKeys.Welcome}}" FontSize="48"/>
      		</StackPanel>
      		<StackPanel Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
      			<Button x:Name="btnEnglish" Tag="en" Content="{I18N {x:Static lang:LangKeys.English}}" Margin="10" Command="{Binding ChangeLanguageCommand}" CommandParameter="{Binding Tag, RelativeSource={RelativeSource Self}}" Width="100" Height="30"/>
      			<Button x:Name="btnLanguage" Tag="zh-CN" Content="{I18N {x:Static lang:LangKeys.Chinese}}" Margin="10" Command="{Binding ChangeLanguageCommand}" CommandParameter="{Binding Tag, RelativeSource={RelativeSource Self}}" Width="100" Height="30"/>
      			<Button Content="{I18N {x:Static lang:LangKeys.ShowMessage}}" Margin="20" Command="{Binding ShowMessageCommand}" Width="150" Height="30"></Button>
      		</StackPanel>
      	</Grid>
      </Window>
      

      viewmodel類:

      using CommunityToolkit.Mvvm.ComponentModel;
      using CommunityToolkit.Mvvm.Input;
      using System;
      using System.Collections.Generic;
      using System.Globalization;
      using System.Linq;
      using System.Text;
      using System.Threading;
      using System.Threading.Tasks;
      using System.Windows;
      using WPFDemoMVVM.Resources;
      using WPFLocalizeExtension.Engine;
      
      namespace WPFDemoMVVM.ViewModel
      {
      	public partial class LanguageChangeViewModel: ObservableObject
      	{
      		[RelayCommand]
      		private void ChangeLanguage(string languageCode)
      		{
      			////第四種方式換語言資源
      			var culture = new CultureInfo(languageCode);
      			Thread.CurrentThread.CurrentCulture = culture;
      			Thread.CurrentThread.CurrentUICulture = culture;
      			I18NExtension.Culture = culture;
      		}
      
      		[RelayCommand]
      		private void ShowMessage()
      		{
      			MessageBox.Show(Lang.Hello,Lang.Prompt, MessageBoxButton.OK, MessageBoxImage.Information);
      		}
      	}
      }
      

      效果如下:
      英文界面:
      image

      中文界面:
      image

      源碼地址:https://gitee.com/chenshibao/wpfdemo.git

      可參考學習開源項目:
      WPFLocalizeExtension:https://github.com/XAMLMarkupExtensions/WPFLocalizeExtension
      Antelcat.I18N:https://github.com/Antelcat/I18N

      如果本文介紹對你有幫助,可以一鍵四連:點贊+評論+收藏+推薦,謝謝!

      posted @ 2025-06-21 23:29  似夢亦非夢  閱讀(419)  評論(0)    收藏  舉報
      主站蜘蛛池模板: 亚洲精品成人综合色在线| 永久免费AV无码网站YY| 精品国产一区av天美传媒| 久久精品国产亚洲av品| 精品偷自拍另类精品在线| 韩国午夜福利片在线观看| 夜夜夜高潮夜夜爽夜夜爰爰| 国产性色的免费视频网站| 日韩人妻无码一区二区三区| 五月综合激情婷婷六月| 一区二区三区四区黄色网| 精品人妻午夜一区二区三区四区| 亚洲一二区在线视频播放| 亚洲精品成人片在线观看精品字幕 | 在线成人国产天堂精品av| 中文字幕在线日韩| 久久亚洲精品国产精品| 亚洲欧美人成网站在线观看看| 永久国产盗摄一区二区色欲| 无码人妻精品一区二区三区蜜桃| 欧洲精品码一区二区三区| 亚洲av午夜成人片| 德令哈市| 中文无码热在线视频| 美女黄网站18禁免费看| 国产SM重味一区二区三区| 国产午夜福利一区二区三区| 亚洲综合精品中文字幕| 国产成人午夜福利在线观看| 亚洲人成色99999在线观看| 曲麻莱县| 国产激情精品一区二区三区| 最近中文字幕国产精品| 国产在线乱子伦一区二区| 欧美刺激性大交| 国产精品国产高清国产av| 国产精品男女爽免费视频| 黄色A级国产免费大片视频| 国产成人高清亚洲综合| 在线观看国产午夜福利片| 中文字幕久久波多野结衣av|