当前位置: 首页 > news >正文

怎么做网站站长软件开发和研发的区别

怎么做网站站长,软件开发和研发的区别,青岛市疾病预防控制中心紧急提示,网站价格评估 优帮云WPF ComboBox 使用 ResourceBinding 动态绑定资源键并支持语言切换独立观察员 2021 年 8 月 23 日我们平常在 WPF 中进行资源绑定操作#xff0c;一般就是用 StaticResource 或者 DynamicResource 后面跟上资源的 key 这种形式#xff0c;能满足大部分需求。但是有的时候一般就是用 StaticResource 或者 DynamicResource 后面跟上资源的 key 这种形式能满足大部分需求。但是有的时候我们需要绑定的是代表了资源的 key 的变量也就是动态绑定资源的 key注意和 DynamicResource 区分开比如本文将要演示的支持国际化的场景。这种动态绑定资源 key 的功能在 WPF 中没有被原生支持所以还是得在网上找找解决方法。 最终在 stackoverflow 网站上看到一篇靠谱的讨论帖Binding to resource key, WPF里面几个人分别用 标记扩展、附加属性、转换器 的方式给出了解决方法本文使用的是 Gor Rustamyan 给出的 标记扩展 的方案核心就是一个 ResourceBinding 类代码整理了下下文给出。 先来看看本次的使用场景吧简单来说就是一个下拉框控件绑定了键值对列表显示的是其中的键但是要求是支持国际化多语言如下图  由于要支持多语言所以键值对的键不是直接显示的值而是显示值的资源键/// summary /// 时间列表 /// /summary public ObservableCollectionKeyValuePairstring, int TimeList { get; set; } 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), };字符串资源放在资源字典中 界面 Xaml 代码为xmlns:markupExtensionsclr-namespace:Mersoft.Mvvm.MarkupExtensionsGroupBox Header演示 ComboBox 绑定资源键国际化支持 Height100StackPanel OrientationHorizontalComboBox MinWidth200 MaxWidth400 Height35 Margin10 FontSize18 VerticalContentAlignmentCenterItemsSource{Binding TimeList} SelectedItem{Binding SelectedTime}ComboBox.ItemTemplateDataTemplateTextBlock Text{markupExtensions:ResourceBinding Key}/TextBlock/DataTemplate/ComboBox.ItemTemplate/ComboBoxButton Width100 Command{Binding SwitchCnCmd} 切换中文 /ButtonButton Width100 Command{Binding SwitchEnCmd} 切换英文 /ButtonTextBlock Text{markupExtensions:ResourceBinding SelectedTime.Key} VerticalAlignmentCenter/TextBlock/StackPanel /GroupBox可以看到给 ComboBox 的 ItemTemplate 设置了一个 DataTemplate里面通过 TextBlock 来绑定键值对中的 Key。关键在于此处不是使用普通的 Binding而是使用了自定义的标记扩展 ResourceBinding其代码如下using System; using System.ComponentModel; using System.Globalization; using System.Windows; using System.Windows.Data; using System.Windows.Markup;namespace Mersoft.Mvvm.MarkupExtensions {/// summary/// 用于处理 绑定代表资源键 (key) 的变量 业务的标记扩展类/// markup extension to allow binding to resourceKey in general case./// https://stackoverflow.com/questions/20564862/binding-to-resource-key-wpf/// /summary/// example/// code/// (Image Source{local:ResourceBinding ImageResourceKey}//// /code/// /examplepublic class ResourceBinding : MarkupExtension{#region Helper propertiespublic static object GetResourceBindingKeyHelper(DependencyObject obj){return (object)obj.GetValue(ResourceBindingKeyHelperProperty);}public static void SetResourceBindingKeyHelper(DependencyObject obj, object value){obj.SetValue(ResourceBindingKeyHelperProperty, value);}// Using a DependencyProperty as the backing store for ResourceBindingKeyHelper. This enables animation, styling, binding, etc...public static readonly DependencyProperty ResourceBindingKeyHelperProperty DependencyProperty.RegisterAttached(ResourceBindingKeyHelper, typeof(object), typeof(ResourceBinding), new PropertyMetadata(null, ResourceKeyChanged));static void ResourceKeyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e){var target d as FrameworkElement;var newVal e.NewValue as Tupleobject, DependencyPropertyif (target null || newVal null)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){var provideValueTargetService (IProvideValueTarget)serviceProvider.GetService(typeof(IProvideValueTarget));if (provideValueTargetService null)return null;if (provideValueTargetService.TargetObject ! null provideValueTargetService.TargetObject.GetType().FullName System.Windows.SharedDp)return this;var targetObject provideValueTargetService.TargetObject as FrameworkElement;var targetProperty provideValueTargetService.TargetProperty as DependencyProperty;if (targetObject null || targetProperty null)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} }主要就是继承 MarkupExtension 并重写 ProvideValue 方法具体的本人也没怎么研究就先不说了大家感兴趣可以自己查一查。这里直接拿来使用可以达到动态绑定资源 key 的目的。 如果使用的是普通的 Binding则只能显示原始值 最后来看看中英文切换当然如果有其它语言也是一样可以切换的。首先是移除现有语言资源的方法/// summary /// 语言名称列表 /// /summary private readonly Liststring _LangKeys new Liststring() { en-us, zh-cn };/// summary /// 移除语言资源 /// /summary /// param nameremoveKeyList 需要移除的资源中包含的 key 的列表默认为空为空移除所有的 /param private void RemoveLangThemes(Liststring removeKeyList null) {if (removeKeyList null){removeKeyList _LangKeys;}var rd Application.Current.Resources;ListResourceDictionary removeList new ListResourceDictionary();foreach (var dictionary in rd.MergedDictionaries){// 判断是否是对应的语言资源文件bool isExists removeKeyList.Exists(x dictionary.Contains(LangName) dictionary[LangName] x);if (isExists){removeList.Add(dictionary);}}foreach (var removeResource in removeList){rd.MergedDictionaries.Remove(removeResource);} }主要是对 Application.Current.Resources.MergedDictionaries 进行操作移除有 LangName 键且值为对应语言代号的资源字典。 然后是应用对应语言资源的方法及调用/// summary /// 应用语言 /// /summary /// param namepackUriTemplate 资源路径模板形如/WPFPractice;component/Resources/Language/{0}.xaml/param /// param namelangName 语言名称形如zh-cn/param private void ApplyLanguage(string packUriTemplate, string langName zh-cn) {var rd Application.Current.Resources;//RemoveLangThemes();var packUri string.Format(packUriTemplate, langName);RemoveLangThemes(new Liststring() { langName });// 将资源加载在最后优先使用rd.MergedDictionaries.Add((ResourceDictionary)Application.LoadComponent(new Uri(packUri, UriKind.Relative))); }/// summary /// 语言资源路径模板字符串 /// /summary private string _LangResourceUriTemplate /WPFPractice;component/Resources/Language/{0}.xaml;/// summary /// 命令方法赋值在构造方法中调用 /// /summary private void SetCommandMethod() {SwitchCnCmd ?? new RelayCommand(o true, async o {ApplyLanguage(_LangResourceUriTemplate, zh-cn);});SwitchEnCmd ?? new RelayCommand(o true, async o {ApplyLanguage(_LangResourceUriTemplate, en-us);}); }逻辑就是先移除要切换到的语言资源的已存在的实例然后将新的实例放在最后以达到比其它语言资源如果有的话更高优先级的目的。 源码地址https://gitee.com/dlgcy/Practice/tree/Blog20210823发行版地址https://gitee.com/dlgcy/Practice/releases/Blog20210823 WPF【翻译】WPF 中附加行为的介绍 Introduction to Attached Behaviors in WPFWPF 使用 Expression Design 画图导出及使用 Path 画图WPF MVVM 弹框之等待框解决 WPF 绑定集合后数据变动界面却不更新的问题使用 ObservableCollectionWPF 消息框 TextBox 绑定新数据时让光标和滚动条跳到最下面真・WPF 按钮拖动和调整大小WPF MVVM 模式下的弹窗WPF 让一组 Button 实现 RadioButton 的当前样式效果WPF 原生绑定和命令功能使用指南WPF 用户控件的自定义依赖属性在 MVVM 模式下的使用备忘在WPF的MVVM模式中使用OCX组件
http://wiki.neutronadmin.com/news/405155/

相关文章:

  • 外包做网站需要多少钱中装建设属于什么板块
  • 东莞网站改版高端网站定制平台
  • 北京市建设工程审核网站scf900色带
  • xp系统做网站服务器吗php开发网站
  • 郑州建设厅官方网站网站建设发文章几点发比较合适
  • 河北建设工程招标投标协会网站软件前端开发主要做什么
  • 成都的网站建设开发公司面包屑导航的网站
  • 多少钱做网站做网站怎么写代码
  • 国外可以做推广的网站吗wordpress 开头空格
  • 深圳做二类医学学分的网站无锡建设市场网站
  • 雪域什么网站是做电影的用ftp做网站
  • 网站空间申请微信平台微商城
  • 小公司做网站的好处自助发稿
  • 成都 广告公司网站建设链接点击量软件
  • 广州商城型网站建设宁波seo网络推广报价
  • 门户网站建设文案怎么做网站上翻译泰剧
  • 贵阳企业网站建设制作怎么模仿别人做网站
  • 网站备案接入商网络广告营销特性
  • 建设网站模块需要哪些群辉装wordpress
  • 整站优化费用网站怎么做图片动态图片大全
  • 家居网站开发项目计划书网站ui设计
  • 网站建设公司文案给公司在百度上做网站
  • 公司快速建站汽车网络营销推广方案
  • 建设银行网站修改用搬瓦工做网站
  • 如何做一个大型网站做最好的网站需要什么
  • 网页网站的区别男女直接做的视频网站免费观看
  • 分销pc网站wordpress 多站点主题
  • 下载wordpress 5.2.1谷歌seo推广服务
  • 怎样制作网站建设规划图微信引流的十个方法
  • 杭州市健康城市建设网站成都网站建设 致尚