尝试合并ValidatableBindableBase
以简化验证实现,我注意到它在Prism.Wpf
.
它在Prism.Windows
(Windows 10 UWP)中可用,但是......
那么我可能错过了它(那么它在哪里)?
或者它真的没有实现WPF
(那么为什么)?
Prism.Wpf 中的验证是通过实现IDataErrorInfo
或INotifyDataErrorInfo
接口完成的。一个例子:
public abstract class DomainObject : INotifyPropertyChanged, INotifyDataErrorInfo
{
private ErrorsContainer<ValidationResult> errorsContainer =
new ErrorsContainer<ValidationResult>(
pn => this.RaiseErrorsChanged( pn ) );
public event EventHandler<DataErrorsChangedEventArgs> ErrorsChanged;
public bool HasErrors
{
get { return this.ErrorsContainer.HasErrors; }
}
public IEnumerable GetErrors( string propertyName )
{
return this.errorsContainer.GetErrors( propertyName );
}
protected void RaiseErrorsChanged( string propertyName )
{
var handler = this.ErrorsChanged;
if (handler != null)
{
handler(this, new DataErrorsChangedEventArgs(propertyName) );
}
}
...
}
这也在Prism的文档中进行了解释。
那么为什么 UWP 不能那样工作呢?因为在 UWP 上您无法访问这些接口,因此需要ValidatableBindableBase
和BindableValidator
类。如果出于某种原因,您喜欢这种方法,没有什么能阻止您使用 UWP 类并将它们带到您的 WPF 解决方案中,所有代码都是开源的。