企业网站建设遵循的原则,网站开发网站制作报价单,crm系统是干什么的,做网站的好处官网#xff1a;https://primslibrary.com 源码地址#xff1a;https://guthub.com/PrismLibrary/prism Prism是由微软发布、维护的开源框架#xff0c;提供了一组设计模式的实现#xff0c;有助于编写结构良好的且可维护的XAML应用程序#xff0c;包括MVVM、依赖注入、命…官网https://primslibrary.com 源码地址https://guthub.com/PrismLibrary/prism Prism是由微软发布、维护的开源框架提供了一组设计模式的实现有助于编写结构良好的且可维护的XAML应用程序包括MVVM、依赖注入、命令、事件聚合器等。 关键程序集 Prism.Core实现MVVM的核心功能是一个与平台无关的项目可以在多个开发平台中使用(Prism.dll)。
如果只需要实现MVVM中的一些简单功能、例如属性变化通知、命令等只需要在Nuget中安装Prism.Core库即可。
Prism.Wpf包含了DialogService、Region、Module、Navigation和其他一些WPF功能使得WPF开发更加方便快捷Prism.Wpf.dll。 如果需要进一步使用WPF中的一些其他功能可以在Nuget中安装Prism.Wpf库由于Prism.Wpf依赖于Prism.Core因此无需再安装Prism.Core。
Prism.Unity包含Prism.Unity.Wpf.dll、Prism.DryIoc.Wpf.dll。 如果需要使用IOC则需要安装Prism.Unity由于Prism.Unity依赖于Prism.Wpf因此不需要再安装Prism.Wpf和Prism.Core
数据处理
一、属性变化通知
Prism框架提供了BindableBase类用于做数据处理例如属性的变化通知等。
五种属性变化通知方式
通过继承BindableBase类可以更加便捷地在WPF中实现属性变化通知具体有如下五种方式。 其中前三种没啥特殊的第四种方式可以在属性变化时通知其他属性的绑定控件而第五种方式则可以在属性发生变化后调用指定的函数。
public class MainViewModel : BindableBase
{private string _value;public string Value{get { return _value; }set {// 第一种方式SetProperty(ref _value, value);// 第二种方式//this.OnPropertyChanged(new System.ComponentModel.PropertyChangedEventArgs(Value));// 第三种方式//this.RaisePropertyChanged();// 第四种方式可以通知另一个属性//SetProperty(ref _value, value, Var);// 第五种方式//SetProperty(ref _value, value, OnValueChanged);}}private void OnValueChanged(){//属性成功变化后的执行函数}
}二、数据异常处理
Prism框架提供了ErrorsContainerT类型专门用于处理项目中出现地异常。其中泛型T为指定的异常消息类型。
1、INotifyDataErrorInfo接口
使用ErrorsContainerT需要实现INotifyDataErrorInfo接口。 实现INotifyDataErrorInfo接口主要需要实现其中的三个成员分别如下
event EventHandlerDataErrorsChangedEventArgs ErrorsChanged事件属性成员用于异常通知。
bool HasErrors属性成员用于判断是否存在异常。
一般会通过ErrorsContainer对象的HasErrors属性来进行判断。
IEnumerable GetErrors(string propertyName)方法成员用于获取相关属性名称的异常。
一般会通过ErrorsContainer对象的GetErrors方法来获得对应属性的异常。
public class MainViewModel :INotifyDataErrorInfo
{//声明ErrorsContainer对象这里没有定义详细做法请看下文public ErrorsContainerstring _errorsContainer;public bool HasErrors _errorsContainer.HasErrors;public event EventHandlerDataErrorsChangedEventArgs ErrorsChanged;public IEnumerable GetErrors(string propertyName){return _errorsContainer.GetErrors(propertyName);}......
}2、ErrorsContainer类
构造函数
ErrorsContainerT(Actionstring raiseErrorsChanged)创建ErrorsContainer对象。
raiseErrorsChanged发生异常时的执行函数函数中要去触发异常发生事件也就是INotifyDataErrorInfo接口的ErrorsChanged事件成员。
常用成员
bool HasErrors属性成员用于判断当前是否存在异常。
IEnumerableT GetErrors(string propertyName)获取指定属性的异常集合。
propertyName要获取异常的属性名称。
SetErrors(string propertyName, IEnumerableT newValidationResults)设置异常也就是发生异常了。
propertyName触发异常的属性名称。newValidationResults异常集合可以是string数组也可以是其他类型数组。
3、具体实现过程
异常处理编写 实现INotifyDataErrorInfo接口
public class MainViewModel : INotifyDataErrorInfo
{public bool HasErrors throw new NotImplementedException();public event EventHandlerDataErrorsChangedEventArgs ErrorsChanged;public IEnumerable GetErrors(string propertyName){throw new NotImplementedException();}
}定义ErrorsContainerT对象属性完善INotifyDataErrorInfo成员实现
public class MainViewModel : INotifyDataErrorInfo
{//定义异常属性private ErrorsContainerstring _errorsContainer;public ErrorsContainerstring ErrorsContainer{get {if (_errorsContainer null){_errorsContainer new ErrorsContainerstring(OnErrorHappend);}return _errorsContainer; }}//当异常发生时触发异常发生事件private void OnErrorHappend(string obj){ErrorsChanged?.Invoke(this, new DataErrorsChangedEventArgs(obj));}public bool HasErrors ErrorsContainer.HasErrors;public event EventHandlerDataErrorsChangedEventArgs ErrorsChanged;public IEnumerable GetErrors(string propertyName){return ErrorsContainer.GetErrors(propertyName);}
}继承BindableBase类定义属性并实现属性变化通知在特定条件下发生异常
public class MainViewModel : BindableBase,INotifyDataErrorInfo
{......//上文的内容private int _value;public int Value{get { return _value; }set {if (value 10){ErrorsContainer.SetErrors(Value, new string[] { 数值不能大于10 });}SetProperty(ref _value, value); }}
}异常消息展示
在xaml中进行异常消息的使用
Window ......Window.DataContextlocal:MainViewModel//Window.DataContextWindow.ResourcesControlTemplate TargetType{x:Type TextBox} x:KeyctGridGrid.RowDefinitionsRowDefinition Heightauto/RowDefinition Heightauto//Grid.RowDefinitionsBorder x:Nameborder BorderBrush{TemplateBinding BorderBrush} BorderThickness{TemplateBinding BorderThickness} Background{TemplateBinding Background} SnapsToDevicePixelsTrueCornerRadius5ScrollViewer x:NamePART_ContentHost Focusablefalse HorizontalScrollBarVisibilityHidden VerticalScrollBarVisibilityHiddenVerticalContentAlignmentCenter Margin3,5 BorderThickness0//BorderTextBlock Grid.Row1 Text{Binding (Validation.Errors)[0].ErrorContent,RelativeSource{RelativeSource AncestorTypeTextBox,ModeFindAncestor}} ForegroundRed Margin10,5NametxtError//GridControlTemplate.TriggersTrigger PropertyValidation.HasError ValueTrueSetter PropertyVisibility ValueVisible TargetNametxtError/Setter PropertyToolTip Value{Binding RelativeSource{RelativeSource Self}, Path(Validation.Errors)[0].ErrorContent}//Trigger/ControlTemplate.Triggers/ControlTemplate/Window.ResourcesGridStackPanelTextBlock Text{Binding Value}/TextBox Text{Binding Value,UpdateSourceTriggerPropertyChanged} Template{StaticResource ct}//StackPanel/Grid
/Window