赞
踩
ResXManager 1.84.6269.0
public class LanguageManager : INotifyPropertyChanged { /// <summary> /// 资源 /// </summary> private readonly ResourceManager _resourceManager; /// <summary> /// 懒加载 /// </summary> private static readonly Lazy<LanguageManager> _lazy = new Lazy<LanguageManager>(() => new LanguageManager()); public static LanguageManager Instance => _lazy.Value; public event PropertyChangedEventHandler PropertyChanged; public LanguageManager() { //获取此命名空间下Resources的Lang的资源,Lang可以修改 _resourceManager = new ResourceManager("WpfAppZBC.Resources.lang", typeof(LanguageManager).Assembly); } /// <summary> /// 索引器的写法,传入字符串的下标 /// </summary> /// <param name="name"></param> /// <returns></returns> /// <exception cref="ArgumentNullException"></exception> public string this[string name] { get { if (name == null) { throw new ArgumentNullException(nameof(name)); } return _resourceManager.GetString(name); } } public void ChangeLanguage(CultureInfo cultureInfo) { CultureInfo.CurrentCulture = cultureInfo; CultureInfo.CurrentUICulture = cultureInfo; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("item[]")); //字符串集合,对应资源的值 } }
<Window x:Class="WpfAppZBC.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:local="clr-namespace:WpfAppZBC" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" Title="{Binding [mainWindow], Source={x:Static local:LanguageManager.Instance}}" Width="800" Height="450" mc:Ignorable="d"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="auto" /> <RowDefinition /> </Grid.RowDefinitions> <Menu> <MenuItem Header="{Binding [file], Source={x:Static local:LanguageManager.Instance}}"> <MenuItem Header="{Binding [open], Source={x:Static local:LanguageManager.Instance}}" /> <MenuItem Header="{Binding [save], Source={x:Static local:LanguageManager.Instance}}" /> <MenuItem Header="{Binding [saveAs], Source={x:Static local:LanguageManager.Instance}}" /> <MenuItem Header="{Binding [exit], Source={x:Static local:LanguageManager.Instance}}" /> </MenuItem> <MenuItem Header="{Binding [edit], Source={x:Static local:LanguageManager.Instance}}"> <MenuItem Header="{Binding [cut], Source={x:Static local:LanguageManager.Instance}}" /> <MenuItem Header="{Binding [copy], Source={x:Static local:LanguageManager.Instance}}" /> <MenuItem Header="{Binding [paste], Source={x:Static local:LanguageManager.Instance}}" /> <MenuItem Header="{Binding [delete], Source={x:Static local:LanguageManager.Instance}}" /> </MenuItem> <MenuItem Header="{Binding [system], Source={x:Static local:LanguageManager.Instance}}"> <MenuItem Header="{Binding [internationalization], Source={x:Static local:LanguageManager.Instance}}"> <MenuItem x:Name="menuItemZHCN" Click="menuItemZHCN_Click" Header="中文简体" /> <MenuItem x:Name="menuItemZHHANT" Click="menuItemZHHANT_Click" Header="中文繁体" /> <MenuItem x:Name="menuItemEN" Click="menuItemEN_Click" Header="English" /> </MenuItem> </MenuItem> </Menu> <TextBlock Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="65" Text="{Binding [contentText], Source={x:Static local:LanguageManager.Instance}}" /> </Grid> </Window>
public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void menuItemZHCN_Click(object sender, RoutedEventArgs e) { LanguageManager.Instance.ChangeLanguage(new CultureInfo("zh-CN")); } private void menuItemZHHANT_Click(object sender, RoutedEventArgs e) { LanguageManager.Instance.ChangeLanguage(new CultureInfo("zh-Hant")); } private void menuItemEN_Click(object sender, RoutedEventArgs e) { LanguageManager.Instance.ChangeLanguage(new CultureInfo("en")); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。