java多语言标签如何动态刷新_WPF实现无刷新动态切换多语言(国际化)
1. 在WPF中國際化使用的是 .xaml文件的格式
如圖:Resource Dictionary (WPF)
2. 創建默認的語言文件和其他語言文件
這里以英語為默認語言,新建一個 Resource Dictionary (WPF)文件,并命名為DefaultLanguage.xaml,內容如下:
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib">
OK
Cancel
默認語言文件的 BuildAction要設置為 Page,如圖:
為了便于管理,一般將所有的語言文件都放在一個目錄下,這里創建lang目錄,
然后在創建另一個語言文件,這里是中文,命名為 zh_CN.xaml,內容如下:
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib">
確定
取消
其他非默認語言的設置應該如下:
BuildAction設置為:Content ;CopyToOutputDirectory設置為:Copy if newer (先這樣做吧,原因未清)
3.在App.xaml中配置默認語言:
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
4.實際使用(敲代碼了)
4.1. 界面效果如下:
4.2. 界面的.xaml代碼
1
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
6
7
8
9
10
11
12
13
14
View Code
4.3. 后臺邏輯代碼:
1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Globalization;4 usingSystem.Linq;5 usingSystem.Text;6 usingSystem.Threading.Tasks;7 usingSystem.Windows;8 usingSystem.Windows.Controls;9 usingSystem.Windows.Data;10 usingSystem.Windows.Documents;11 usingSystem.Windows.Input;12 usingSystem.Windows.Media;13 usingSystem.Windows.Media.Imaging;14 usingSystem.Windows.Navigation;15 usingSystem.Windows.Shapes;16
17 namespaceLanTest18 {19 ///
20 ///Interaction logic for MainWindow.xaml21 ///
22 public partial classMainWindow : Window23 {24 publicMainWindow()25 {26 InitializeComponent();27 }28
29 //定義ComboBox選項的類,存放Name和Value
30 public classCategoryInfo31 {32 public stringName33 {34 get;35 set;36 }37 public stringValue38 {39 get;40 set;41 }42
43 }44
45 //切換語言
46 private void btnChangeLang_Click(objectsender, RoutedEventArgs e)47 {48 object selectedName =cbLang.SelectedValue;49 if (selectedName != null)50 {51 string langName =selectedName.ToString();52 //英語的語言文件名為:DefaultLanguage,所有這里要轉換一下
53 if (langName == "en_US")54 langName = "DefaultLanguage";55 //根據本地語言來進行本地化,不過這里上不到56 //CultureInfo currentCultureInfo = CultureInfo.CurrentCulture;
57
58 ResourceDictionary langRd = null;59 try
60 {61 //根據名字載入語言文件
62 langRd = Application.LoadComponent(new Uri(@"lang\" + langName + ".xaml", UriKind.Relative)) asResourceDictionary;63 }64 catch(Exception e2)65 {66 MessageBox.Show(e2.Message);67 }68
69 if (langRd != null)70 {71 //如果已使用其他語言,先清空
72 if (this.Resources.MergedDictionaries.Count > 0)73 {74 this.Resources.MergedDictionaries.Clear();75 }76 this.Resources.MergedDictionaries.Add(langRd);77 }78 }79 else
80 MessageBox.Show("Please selected one Language first.");81 }82
83 //控件載入時,為ComboBox賦值
84 private void cbLang_Loaded(objectsender, RoutedEventArgs e)85 {86 List categoryList = new List();87 categoryList.Add(new CategoryInfo() { Name = "English", Value = "en_US"});88 categoryList.Add(new CategoryInfo() { Name = "中文", Value = "zh_CN"});89
90 cbLang.ItemsSource = categoryList;//綁定數據,真正的賦值
91 cbLang.DisplayMemberPath = "Name";//指定顯示的內容
92 cbLang.SelectedValuePath = "Value";//指定選中后的能夠獲取到的內容
93 }94 }95 }
View Code
總結
以上是生活随笔為你收集整理的java多语言标签如何动态刷新_WPF实现无刷新动态切换多语言(国际化)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 为什么快餐店硬椅子?
- 下一篇: java xsl生成word文件_导出生