ReactiveUI和验证

时间:2011-08-18 23:51:50

标签: silverlight validation silverlight-4.0 reactiveui reactivevalidatedobject

在使用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);
    }
}

以下是我要验证的内容:

  1. 如果SendEmail == true,请确保电子邮件属性中有有效的电子邮件地址。 (我并不担心实际的电子邮件地址验证部分。这只是一个如果的情况。)
  2. 如果将值设置为Email属性,请确保它是有效的电子邮件地址。
  3. 如果1.或2.验证失败,则SaveMyDataCommand不应该是可执行的。
  4. 我正在寻找一个关于如何使用ReactiveUI进行简单/稍微复杂的数据验证的好例子。任何人都可以对此有所了解吗?

2 个答案:

答案 0 :(得分:3)

对于其他寻找使用ReactiveValidatedObject示例的人来说,这对我有用。请注意,您还必须向班级添加System.ComponentModel的引用。

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);
  }
}

我希望这有助于某人! :)

答案 1 :(得分:1)

使用ReactiveValidatedObject,然后使用数据注释(在电话上,抱歉短信)