优秀网站设计 打造有吸引力的网站,寮步网站仿做,九一制作网站,用网站素材做logoWPF 本地化的最佳做法 资源文件英文资源文件 en-US.xaml中文资源文件 zh-CN.xaml 资源使用App.xaml主界面布局cs代码 App.config辅助类语言切换操作类资源 binding 解析类 实现效果 应用程序本地化有很多种方式#xff0c;选择合适的才是最好的。这里只讨论一种方式#xff0… WPF 本地化的最佳做法 资源文件英文资源文件 en-US.xaml中文资源文件 zh-CN.xaml 资源使用App.xaml主界面布局cs代码 App.config辅助类语言切换操作类资源 binding 解析类 实现效果 应用程序本地化有很多种方式选择合适的才是最好的。这里只讨论一种方式动态资源DynamicResource 这种方式可是在不重启应用程序的情况下进行资源的切换不论是语言切换还是更上层的主题切换。想要运行时切换不同的资源就必须使用 动态资源DynamicResource 这种方式。 图片是可以使用资源字典进行动态 binding 的 不要被误导了 资源文件 确保不同语言环境中资源 Key 是同一个且对用的资源类型是相同的。 在资源比较多的情况下可以通过格式化的命名方式来规避 Key 冲突。 资源文件的属性可以设置成 生成操作内容 复制到输出目录如果较新则复制 上面的操作可以在不重新编译程序的情况下编辑语言资源并应用。 如果不想让别人更改语言资源则 生成操作页 复制到输出目录不复制 英文资源文件 en-US.xaml
ResourceDictionaryxmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:sysclr-namespace:System;assemblymscorlibsys:String x:KeyWindowsTitleMainWindow/sys:StringImageSource x:Keyiconpack://SiteOfOrigin:,,,/Icons/en-USicon.png/ImageSourcesys:String x:KeyLanguageButtonLanguageButton/sys:Stringsys:String x:KeyLockTime-OneMinute1 Minute/sys:Stringsys:String x:KeyLockTime-FiveMinute5 Minute/sys:Stringsys:String x:KeyLockTime-TenMinute10 Minute/sys:Stringsys:String x:KeyLockTime-FifteenMinute15 Minute/sys:Stringsys:String x:KeyLockTime-ThirtyMinute30 Minute/sys:Stringsys:String x:KeyLockTime-OneHour1 Hour/sys:Stringsys:String x:KeyLockTime-TwoHour2 Hour/sys:Stringsys:String x:KeyLockTime-ThreeHour3 Hour/sys:Stringsys:String x:KeyLockTime-NeverNever/sys:String
/ResourceDictionary中文资源文件 zh-CN.xaml
ResourceDictionaryxmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:sysclr-namespace:System;assemblymscorlibsys:String x:KeyWindowsTitle主窗口/sys:StringImageSource x:Keyiconpack://SiteOfOrigin:,,,/Icons/zh-CNicon.png/ImageSourcesys:String x:KeyLanguageButton语言按钮/sys:Stringsys:String x:KeyLockTime-OneMinute1 分钟/sys:Stringsys:String x:KeyLockTime-FiveMinute5 分钟/sys:Stringsys:String x:KeyLockTime-TenMinute10 分钟/sys:Stringsys:String x:KeyLockTime-FifteenMinute15 分钟/sys:Stringsys:String x:KeyLockTime-ThirtyMinute30 分钟/sys:Stringsys:String x:KeyLockTime-OneHour1 小时/sys:Stringsys:String x:KeyLockTime-TwoHour2 小时/sys:Stringsys:String x:KeyLockTime-ThreeHour3 小时/sys:Stringsys:String x:KeyLockTime-Never永不/sys:String
/ResourceDictionary资源使用
App.xaml 设置默认语言资源用于在设计阶段预览并利用 VS 智能提示资源的 Key 防止 Key编写出错。 Applicationx:ClassLocalization.Core.Appxmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:localclr-namespace:Localization.CoreStartupUriMainWindow.xamlApplication.ResourcesResourceDictionaryResourceDictionary.MergedDictionariesResourceDictionary Source/I18nResources/en-US.xaml //ResourceDictionary.MergedDictionaries/ResourceDictionary/Application.Resources
/Application主界面布局
Windowx:ClassLocalization.Core.MainWindowxmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:dhttp://schemas.microsoft.com/expression/blend/2008xmlns:localclr-namespace:Localization.Corexmlns:mchttp://schemas.openxmlformats.org/markup-compatibility/2006xmlns:operateclr-namespace:Localization.Core.OperatesTitle{DynamicResource WindowsTitle}Width800Height450Icon{DynamicResource icon}mc:IgnorabledGridGrid.RowDefinitionsRowDefinition HeightAuto /RowDefinition //Grid.RowDefinitionsComboBoxx:NamecomLanWidth{Binding SizeToContent.Width}Height{Binding SizeToContent.Width}HorizontalAlignmentCenterVerticalAlignmentCenterSelectedValuePathTagSelectionChangedComboBox_SelectionChangedComboBoxItem Content中文 Tagzh-CN /ComboBoxItem ContentEnglish Tagen-US //ComboBoxStackPanel Grid.Row1ButtonMargin0,50,0,0HorizontalAlignmentCenterVerticalAlignmentCenterContent{DynamicResource LanguageButton} /ComboBoxx:NamecmTimeWidth120Margin20VerticalAlignmentCenterComboBox.ItemTemplateDataTemplateTextBlock Text{operate:ResourceBinding Key} //DataTemplate/ComboBox.ItemTemplate/ComboBox/StackPanel/Grid
/Windowcs代码
public partial class MainWindow : Window
{ObservableCollectionKeyValuePairstring, int? TimeList { get; set; }public MainWindow(){InitializeComponent();var lan Thread.CurrentThread.CurrentCulture.Name;comLan.SelectedValue lan;Loaded MainWindow_Loaded;}private void MainWindow_Loaded(object sender, RoutedEventArgs e){TimeList new ObservableCollectionKeyValuePairstring, int(){new KeyValuePairstring, int(LockTime-OneMinute, 1),new KeyValuePairstring, int(LockTime-FiveMinute, 5),new KeyValuePairstring, int(LockTime-TenMinute, 10),new KeyValuePairstring, int(LockTime-FifteenMinute, 15),new KeyValuePairstring, int(LockTime-ThirtyMinute, 30),new KeyValuePairstring, int(LockTime-OneHour, 60),new KeyValuePairstring, int(LockTime-TwoHour, 120),new KeyValuePairstring, int(LockTime-ThreeHour, 180),new KeyValuePairstring, int(LockTime-Never, 0),};cmTime.ItemsSource TimeList;cmTime.SelectedValue TimeList[0];}private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e){if (e.AddedItems is null) return;LanguageOperate.SetLanguage((e.AddedItems[0] as ComboBoxItem)!.Tag.ToString()!);}
}App.config language 配置项用于保存用户选择的语言类型 ?xml version1.0 encodingutf-8 ?
configurationappSettingsadd keylanguage value//appSettings
/configuration辅助类
语言切换操作类 检查方法入参有值则切换到指定的资源无值则读取配置文件中的值若配置文件中仍无值则获取当前线程运行的语言环境切换到与语言环境相匹配的资源如果没有找到与之匹配的资源则不做操作。 切换资源之后将配置文件中的 language 的 value 改为当前所选的语言保存并刷新配置文件直到下次更改。 重写 Application 类的 OnStartup 方法在 OnStartup 中调用 SetLanguage 方法以实现应用程序启动对语言环境的判别。 /// summary
/// 语言操作
/// /summary
public class LanguageOperate
{private const string KEY_OF_LANGUAGE language;/// summary/// 语言本地化/// /summary/// param namelanguage语言环境/parampublic static void SetLanguage(string language ){if (string.IsNullOrWhiteSpace(language)){language ConfigurationManager.AppSettings[KEY_OF_LANGUAGE]!;if (string.IsNullOrWhiteSpace(language))language System.Globalization.CultureInfo.CurrentCulture.ToString();}string languagePath $I18nResources\{language}.xaml;if (!System.IO.File.Exists(languagePath)) return;var lanRd Application.LoadComponent(new Uri(languagePath, UriKind.RelativeOrAbsolute)) as ResourceDictionary;var old Application.Current.Resources.MergedDictionaries.FirstOrDefault(o o.Contains(WindowsTitle));if (old ! null)Application.Current.Resources.MergedDictionaries.Remove(old);Application.Current.Resources.MergedDictionaries.Add(lanRd);Configuration configuration ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);configuration.AppSettings.Settings[KEY_OF_LANGUAGE].Value language;configuration.Save();ConfigurationManager.RefreshSection(AppSettings);var culture new System.Globalization.CultureInfo(language);System.Globalization.CultureInfo.CurrentCulture culture;System.Globalization.CultureInfo.CurrentUICulture culture;}
}资源 binding 解析类 ComBox 控件通常是通过 ItemSource 进行绑定默认情况下是无法对绑定的资源进行翻译的。 通过继承 MarkupExtension 类 重写 ProvideValue 方法来实现item 绑定 资源的 Key 的解析。 /// summary
/// markup extension to allow binding to resourceKey in general case
/// /summary
public class ResourceBinding : MarkupExtension
{#region propertiesprivate static DependencyObject resourceBindingKey;public static DependencyObject ResourceBindingKey{get resourceBindingKey;set resourceBindingKey value;}// Using a DependencyProperty as the backing store for ResourceBindingKeyHelper. This enables animation, styling, binding, etc...public static readonly DependencyProperty ResourceBindingKeyHelperProperty DependencyProperty.RegisterAttached(nameof(ResourceBindingKey),typeof(object),typeof(ResourceBinding),new PropertyMetadata(null, ResourceKeyChanged));static void ResourceKeyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e){if (!(d is FrameworkElement target) || !(e.NewValue is Tupleobject, DependencyProperty newVal)) return;var dp newVal.Item2;if (newVal.Item1 null){target.SetValue(dp, dp.GetMetadata(target).DefaultValue);return;}target.SetResourceReference(dp, newVal.Item1);}#endregionpublic ResourceBinding() { }public ResourceBinding(string path) Path new PropertyPath(path);public override object ProvideValue(IServiceProvider serviceProvider){if ((IProvideValueTarget)serviceProvider.GetService(typeof(IProvideValueTarget)) null) return null;if (((IProvideValueTarget)serviceProvider.GetService(typeof(IProvideValueTarget))).TargetObject ! null ((IProvideValueTarget)serviceProvider.GetService(typeof(IProvideValueTarget))).TargetObject.GetType().FullName is System.Windows.SharedDp) return this;if (!(((IProvideValueTarget)serviceProvider.GetService(typeof(IProvideValueTarget))).TargetObject is FrameworkElement targetObject) || !(((IProvideValueTarget)serviceProvider.GetService(typeof(IProvideValueTarget))).TargetProperty is DependencyProperty targetProperty)) return null;#region bindingBinding binding new Binding{Path Path,XPath XPath,Mode Mode,UpdateSourceTrigger UpdateSourceTrigger,Converter Converter,ConverterParameter ConverterParameter,ConverterCulture ConverterCulture,FallbackValue FallbackValue};if (RelativeSource ! null)binding.RelativeSource RelativeSource;if (ElementName ! null)binding.ElementName ElementName;if (Source ! null)binding.Source Source;#endregionvar multiBinding new MultiBinding{Converter HelperConverter.Current,ConverterParameter targetProperty};multiBinding.Bindings.Add(binding);multiBinding.NotifyOnSourceUpdated true;targetObject.SetBinding(ResourceBindingKeyHelperProperty, multiBinding);return null;}#region Binding Members/// summary/// The source path (for CLR bindings)./// /summarypublic object Source { get; set; }/// summary/// The source path (for CLR bindings)./// /summarypublic PropertyPath Path { get; set; }/// summary/// The XPath path (for XML bindings)./// /summary[DefaultValue(null)]public string XPath { get; set; }/// summary/// Binding mode/// /summary[DefaultValue(BindingMode.Default)]public BindingMode Mode { get; set; }/// summary/// Update type/// /summary[DefaultValue(UpdateSourceTrigger.Default)]public UpdateSourceTrigger UpdateSourceTrigger { get; set; }/// summary/// The Converter to apply/// /summary[DefaultValue(null)]public IValueConverter Converter { get; set; }/// summary/// The parameter to pass to converter./// /summary/// value/value[DefaultValue(null)]public object ConverterParameter { get; set; }/// summary/// Culture in which to evaluate the converter/// /summary[DefaultValue(null)][TypeConverter(typeof(System.Windows.CultureInfoIetfLanguageTagConverter))]public CultureInfo ConverterCulture { get; set; }/// summary/// Description of the object to use as the source, relative to the target element./// /summary[DefaultValue(null)]public RelativeSource RelativeSource { get; set; }/// summary/// Name of the element to use as the source/// /summary[DefaultValue(null)]public string ElementName { get; set; }#endregion#region BindingBase Members/// summary/// Value to use when source cannot provide a value/// /summary/// remarks/// Initialized to DependencyProperty.UnsetValue; if FallbackValue is not set, BindingExpression/// will return target propertys default when Binding cannot get a real value./// /remarkspublic object FallbackValue { get; set; }#endregion#region Nested typesprivate class HelperConverter : IMultiValueConverter{public static readonly HelperConverter Current new HelperConverter();public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture){return Tuple.Create(values[0], (DependencyProperty)parameter);}public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture){throw new NotImplementedException();}}#endregion
}实现效果