ReactiveUI和验证 [英] ReactiveUI and Validation
问题描述
使用ReactiveUI
进行数据验证时,什么被认为是最佳实践"?假设我有一个看起来像这样的视图模型:
What's considered "best practice" when performing data validation while using ReactiveUI
? Let's say I have a view model that looks like this:
public class MyViewModel: ReactiveObject
{
public ReactiveAsyncCommand SaveMyDataCommand { get; protected set; }
private string _email;
public string Email
{
get { return _email; }
set
{
_email = value;
raisePropertyChanged("Email");
}
}
private string _name;
public string Name
{
get { return _name; }
set
{
_name= value;
raisePropertyChanged("Name");
}
}
private bool _sendEmail = false;
public bool SendEmail
{
get { return _sendEmail; }
set
{
_sendEmail = value;
raisePropertyChanged("SendEmail");
}
}
public MyViewModel()
{
SaveMyDataCommand = new ReactiveAsyncCommand(null, 1);
}
}
这是我要验证的内容:
- 如果为
SendEmail == true
,请确保在电子邮件"属性中有一个有效的电子邮件地址. (我不担心实际的电子邮件地址验证部分本身.这只是一个假设场景.) - 如果将值设置为
Email
属性,请确保它是有效的电子邮件地址. - 如果1.或2.验证失败,则
SaveMyDataCommand
不应执行.
- If
SendEmail == true
then make sure there's a valid email address in the Email property. (I'm not worried about the actual email address validation piece itself. This is just a what if scenario.) - If a value was set to the
Email
property, make sure it's a valid email address. - If either 1. or 2. fail validation,
SaveMyDataCommand
should not be executable.
我只是在寻找一个很好的示例,说明如何使用ReactiveUI进行简单/稍微复杂的数据验证.谁能对此有所启发?
I'm just looking for a good example on how to do simple / slightly more complex data validation using ReactiveUI. Can anyone shed some light on this?
推荐答案
对于其他正在寻找使用ReactiveValidatedObject
的示例的人,这对我有用.请注意,您还必须在类中添加对System.ComponentModel
的引用.
For anyone else looking for an example on using ReactiveValidatedObject
, here's what worked for me. Note that you'll have to add a reference to System.ComponentModel
to your class as well.
public class MyViewModel: ReactiveValidatedObject
{
public ReactiveAsyncCommand SaveMyDataCommand { get; protected set; }
// THIS PROPERTY INDICATES WHETHER ALL FIELDS HAVE BEEN VALIDATED
public bool IsSaveEnabled
{
get { return IsObjectValid(); }
}
private string _email;
[ValidatesViaMethod(AllowBlanks=true,AllowNull=true,Name="IsEmailValid",ErrorMessage="Please enter a valid email address")]
public string Email
{
get { return _email; }
set
{
_email = value;
raisePropertyChanged("Email");
SendEmail = SendEmail;
raisePropertyChanged("IsSaveEnabled");
}
}
private string _name;
public string Name
{
get { return _name; }
set
{
_name= value;
raisePropertyChanged("Name");
}
}
private bool _sendEmail = false;
[ValidatesViaMethod(Name = "IsSendEmailValid", ErrorMessage = "Make sure a valid email address is entered.")]
public bool SendEmail
{
get { return _sendEmail; }
set
{
_sendEmail = value;
raisePropertyChanged("SendEmail");
raisePropertyChanged("IsSaveEnabled");
}
}
public bool IsEmailValid(string email)
{
if (string.IsNullOrEmpty(email))
{
return true;
}
// Return result of Email validation
}
public bool IsSendEmailValid(bool sendEmail)
{
if (sendEmail)
{
if (string.IsNullOrEmpty(Email))
{
return false;
}
// Return result of Email validation
}
}
public MyViewModel()
{
SaveMyDataCommand = new ReactiveAsyncCommand(null, 1);
}
}
我希望这对某人有帮助! :)
I hope this helps someone! :)
这篇关于ReactiveUI和验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!