|
1. 引言
在.NET开发领域,FluentValidation以其优雅、易扩展的特性成为开发者进行属性验证的首选工具。它不仅适用于Web开发,如MVC、Web API和ASP.NET CORE,同样也能完美集成在WPF应用程序中,提供强大的数据验证功能。本文将深入探讨如何在C# WPF项目中运用FluentValidation进行属性验证,并展示如何通过MVVM模式实现这一功能。
2. 功能概览
我们的目标是构建一个WPF应用程序,它能够通过FluentValidation实现以下验证功能:
- 验证ViewModel层的基本数据类型属性,如int、string等。
- 对ViewModel中的复杂属性进行验证,这包括对象属性的子属性以及集合属性。
- 提供两种直观的错误提示样式,以增强用户体验。
先看实现效果图:
3. 解决问题与探索
在调研过程中,我发现FluentValidation官方文档主要关注于Web应用的验证。对于WPF和复杂属性的验证,官方文档提供的示例有限。然而,通过深入研究和实践,我找到了将FluentValidation与WPF结合使用的有效方法,特别是针对复杂属性的验证。
4. 开发步骤
4.1. 创建工程、库引入
首先,创建一个新的WPF项目,并引入FluentValidation库用于属性验证,以及Prism.Wpf库以简化MVVM模式的实现。- <ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>
复制代码 4.2. 创建实体类
我创建了两个实体类:Student和Field,分别代表对象属性和集合项属性。这两个类都实现了IDataErrorInfo接口:
- IDataErrorInfo接口常用于提供实体数据验证的错误信息。这个接口包含两个成员:一个索引器(this[string columnName])和一个Error属性。索引器用于按属性名称提供错误信息,而Error属性则用于提供整个实体的错误概述。
- 两个实体类和另外在后面提及的ViewModel中也实现IDataErrorInfo接口,并在this[string columnName]索引器和Error属性中使用FluentValidation来验证属性。
4.2.1. 普通类 - Student
学生类包含5个属性:名字、年龄、邮政编码、最小值和最大值,其中最小值和最大值涉及关联验证,即最小值变化后通知最大值验证,反之同理。上面关键代码在public string this[string columnName]:这里进行输入表单项的数据校验,FluentValidation调用就在这里,校验逻辑封装在StudentValidator,表单输入时会实时调用该处代码,columnName表示表单项的列名,就是View绑定的属性名。
4.2.2. 集合类 - Field
此类用作ViewModel中的集合项使用,模拟动态表单数据校验,简单包含4个属性:字段名称、字段显示名称、数据类型、数据值,表单主要根据数据类型验证输入的数据值是否合法。同样此实体需要继承IDataErrorInfo接口,用于触发FluentValidation验证使用。看上面代码,public string this[string columnName]代码处写法和Student类一样,只是_validator变量类型不同,前者为StudentValidator,这里是FieldValidator,下面我们看看这两个类怎么写。
4.3. 创建验证器
对于每个实体类,我都创建了一个对应的验证器类:StudentValidator和FieldValidator。这些验证器类继承自AbstractValidator,并在其中定义了验证规则。验证属性的写法有两种:
- 可以在实体属性上方添加特性(本文不作特别说明,百度文章介绍很多);
- 通过代码的形式添加,如下方,创建一个验证器类,继承自AbstractValidator,在此验证器构造函数中写规则验证属性,方便管理。
本文使用第二种,下面通过创建StudentValidator和FieldValidator两个验证器类介绍。
4.3.1. StudentValidator
这是学生验证器StudentValidator,需要继承AbstractValidator,泛型指定前面需要验证的实体类Student:代码简单,使用到数字的大小和范围验证(见Age)、字符串不能为空和长度限制(见Name)、字符串正则表达式验证(见Zip)、多属性关联验证(最小值和最大值,这里配合属性set时通知其他属性验证通知RaisePropertyChanged(nameof(MaxValue));)。
4.3.2. FieldValidator
动态表单数据值校验器,同理需要继承AbstractValidator,泛型指定前面需要验证的实体类Field::- public class FieldValidator : AbstractValidator{<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>public FieldValidator()<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>{<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>RuleFor(field => field.Value)<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>.Must((field, value) => (field.Type == DataType.Text && !string.IsNullOrWhiteSpace(value))<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>|| (field.Type == DataType.Number && double.TryParse(value, out _))<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>|| (field.Type == DataType.Date && DateTime.TryParse(value, out _)))<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>.WithMessage("1.文本不能为空;2.数字类型请填写数字;3.日志类型请填写日期类型");<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>}}
复制代码 这里写的简单了点:
- 文本数据类型,值不能为空;
- 数字数据类型,必须是double类型;
- 日期类型,必须能使用DateTime转换;
本文只做简单演示,多种数据类型放Must方法中做统一验证,验证出错给出统一的提示信息,读者可按实际情况修改。
4.3.3. StudentViewModelValidator
此外,我还创建了一个StudentViewModelValidator,用于验证ViewModel层的属性。这个验证器能够处理基本数据类型、对象属性以及集合属性的验证。- public class StudentViewModelValidator : AbstractValidator{<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>public StudentViewModelValidator()<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>{<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>RuleFor(vm => vm.Title)<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>.NotEmpty()<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>.WithMessage("标题长度不能为空!")<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>.Length(5, 30)<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>.WithMessage("标题长度限制在5到30个字符之间!");<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>RuleFor(vm => vm.CurrentStudent).SetValidator(new StudentValidator());<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>RuleForEach(vm => vm.Fields).SetValidator(new FieldValidator());<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>}}
复制代码
- Title用于关联验证基本数据类型(string类型);
- CurrentStudent用于验证对象属性(Student类的实例),设置验证该属性时使用StudentValidator验证器;
- Fields用于验证集合属性(ObservableCollection),设置验证该属性子项时使用FieldValidator验证器,注意前面使用的RuleForEach表示关联集合中的项验证器。
4.4. ViewModel层实现
StudentViewModel与Student实体类结构类似,都需要实现IDataErrorInfo接口,该类由一个简单的string属性(Title)和一个复杂的Student对象属性(CurrentStudent)、集合属性ObservableCollection Fields组成,代码如下:- /// ///<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup> 视图ViewModel///<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup> 继承BindableBase,即继承属性变化接口INotifyPropertyChanged///<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup> 实现IDataErrorInfo接口,用于FluentValidation验证,必须实现此接口/// public class StudentViewModel : BindableBase, IDataErrorInfo{<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>private Student _currentStudent;<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>private string _title;<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>private readonly StudentViewModelValidator _validator;<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>public string Title<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>{<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>get => _title;<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>set => SetProperty(ref _title, value);<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>}<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>public Student CurrentStudent<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>{<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>get => _currentStudent;<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>set => SetProperty(ref _currentStudent, value);<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>}<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>public ObservableCollection Fields { get; } = new();<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>private DelegateCommand _saveCommand;<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>public DelegateCommand SaveCommand => _saveCommand ??= new DelegateCommand(HandleSaveCommand,<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>HandleCanExecuteSaveCommand);<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>private DelegateCommand _cancelCommand;<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>public DelegateCommand CancelCommand =><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>_cancelCommand ??= new DelegateCommand(HandleCancelCommand, () => true);<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>public StudentViewModel()<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>{<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>_validator = new StudentViewModelValidator();<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>CurrentStudent = new Student<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>{<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>Name = "李刚的儿",<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>Age = 23<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>};<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>Fields.Add(new Field(DataType.Text, "文本,比如:四川省成都市场", "地址", ""));<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>Fields.Add(new Field(DataType.Number, "数字,比如:12", "工龄", ""));<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>Fields.Add(new Field(DataType.Date, "时间,比如:2023-09-26 05:13:23", "培训时间", ""));<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>PropertyChanged += Validate;<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>CurrentStudent.PropertyChanged += Validate;<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>foreach (var field in Fields)<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>{<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>field.PropertyChanged += Validate;<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>}<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>}<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>~StudentViewModel()<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>{<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>PropertyChanged -= Validate;<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>CurrentStudent.PropertyChanged -= Validate;<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>foreach (var field in Fields)<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>{<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>field.PropertyChanged -= Validate;<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>}<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>}<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>private void Validate(object sender, PropertyChangedEventArgs e)<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>{<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>_isCanExecuteSaveCommand = _validator.Validate(this).IsValid;<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>SaveCommand.RaiseCanExecuteChanged();<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>}<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>private void HandleSaveCommand()<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>{<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>var validateResult = _validator.Validate(this);<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>if (validateResult.IsValid)<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>{<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>MessageBox.Show("看到我说明验证成功!");<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>}<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>else<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>{<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>var errorMsg = string.Join(Environment.NewLine,<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>validateResult.Errors.Select(x => x.ErrorMessage).ToArray());<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>MessageBox.Show($"慌啥子嘛,你再检查下输入噻:\r\n{errorMsg}");<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>}<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>}<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>private bool _isCanExecuteSaveCommand;<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>private bool HandleCanExecuteSaveCommand()<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>{<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>return _isCanExecuteSaveCommand;<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>}<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>private void HandleCancelCommand()<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>{<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>MessageBox.Show("我啥都不做,退休了");<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>}<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>public string this[string columnName]<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>{<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>get<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>{<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>var validateResult = _validator.Validate(this);<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>if (validateResult.IsValid)<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>{<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>return string.Empty;<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>}<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>var firstOrDefault =<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>validateResult.Errors.FirstOrDefault(error => error.PropertyName == columnName);<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>return firstOrDefault == null ? string.Empty : firstOrDefault.ErrorMessage;<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>}<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>}<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>public string Error<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>{<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>get<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>{<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>var validateResult = _validator.Validate(this);<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>if (validateResult.IsValid)<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>{<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>return string.Empty;<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>}<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>var errors = string.Join(Environment.NewLine, validateResult.Errors.Select(x => x.ErrorMessage).ToArray());<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>return errors;<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>}<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>}}
复制代码 ViewModel属性验证和Student及Field类似,这里我加上了保存(SaveCommand)和取消(CancelCommand)两个命令,其中保存命令需要所有属性验证通过才可用,通过注册属性的变化事件PropertyChanged,在变化事件处理程序中验证:- PropertyChanged += Validate;CurrentStudent.PropertyChanged += Validate;foreach (var field in Fields){<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>field.PropertyChanged += Validate;}
复制代码- private void Validate(object sender, PropertyChangedEventArgs e){<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>_isCanExecuteSaveCommand = _validator.Validate(this).IsValid;<ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>SaveCommand.RaiseCanExecuteChanged();}
复制代码 4.5. 视图层实现
在视图层,我创建了一个用户控件StudentView,用于显示输入表单和验证结果。通过绑定ViewModel层的属性和命令,视图层能够与ViewModel层进行交互,并实时显示验证错误。这里比较简单,提供简单属性标题(Title)、复杂属性(包括学生姓名(CurrentStudent.Name)、学生年龄( CurrentStudent .Age)、学生邮政编码( CurrentStudent .Zip)、最小值(CurrentStudent.MinValue)、最大值(CurrentStudent.MaxValue))验证、集合属性验证(Fields),xaml代码如下:4.6. 错误提示样式
为了提升用户体验,我定义了两种错误提示样式:一种是通过红色图标提示输入框旁边的错误,另一种是在输入框右侧显示错误文字。这些样式定义在App.xaml中,并可以在整个应用程序中复用。- <ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup><ItemGroup>
- <PackageReference Include="FluentValidation" Version="11.9.0" />
- <PackageReference Include="Prism.Wpf" Version="9.0.271-pre" />
- </ItemGroup>
复制代码 5. 效果展示
通过上述步骤的实现,我们得到了一个功能完善的WPF应用程序。它能够根据用户输入实时进行验证,并提供直观的错误提示。当所有属性都验证通过时,提交按钮将变为可用状态。
6. 源码分享
为了方便读者学习和交流,本文将所有代码同步到了Gitee和Github平台上。欢迎感兴趣的开发者访问以下链接获取源码:
7. 总结
通过本文的介绍和实践,我们成功将FluentValidation应用于C# WPF项目中,实现了对ViewModel层属性的全面验证。这不仅提升了数据的安全性和准确性,也为用户提供了更好的交互体验。希望本文能对广大开发者在WPF项目中使用FluentValidation提供有益的参考和启示。
参考:
来源:https://www.cnblogs.com/Dotnet9-com/p/17986383
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作! |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?立即注册
x
|