ReactiveUI 和验证
使用 ReactiveUI
执行数据验证时什么被认为是“最佳实践”?假设我有一个如下所示的视图模型:
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 属性中有有效的电子邮件地址。 (我并不担心实际的电子邮件地址验证部分本身。这只是一个假设场景。) - 如果将值设置为
Email
属性,请确保这是一个有效的电子邮件地址。 - 如果 1. 或 2. 验证失败,则
SaveMyDataCommand
不应可执行。
我只是在寻找一个关于如何使用 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);
}
}
Here's what I want to validate:
- 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.
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于其他正在寻找使用 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 toSystem.ComponentModel
to your class as well.I hope this helps someone! :)
使用ReactiveValidatedObject,然后使用Data Annotations(在电话上,抱歉短信)
Use ReactiveValidatedObject, then use Data Annotations (on phone, sorry for short message)