我已经实现了如下的异步验证。本文.
我在我的两篇文章中都使用了数据注释。Organization
班级和我 Address
类。 组织有 OrgnizationName
属性(字符串)地址属性。 相关位是
Address.cs
private string city;
[Required]
public string City
{
get { return this.city; }
set
{
if (this.city != value)
{
this.city = value;
RaisePropertyChanged("City");
}
}
}
// Other Address bits...
Organization.cs
private string organizationName;
[Required]
[StringLength(50)]
[Display(Name = "Organization")]
public string OrganizationName
{
get { return this.organizationName; }
set
{
if (this.organizationName != value)
{
this.organizationName = value;
RaisePropertyChanged("OrganizationName");
}
}
}
[Required]
[Display(Name = "Address")]
public Address Address { get; set; } = new Address();
// Other Organization bits...
我的VM是
OrganizationVM.cs
public class OrganizationDetailsVM : ValidatableModel
{
public Organization Organization { get; set; }
public OrganizationDetailsVM (Organization inputOrganization)
{
Organization = inputOrganization;
// other bits here...
}
public Command SaveCommand
{
get
{
return new Command(() =>
{
Debug.Print("Saving Organization...");
try
{
if (Organization.OrganizationID == 0)
Organization.AddOrganization();
else
Organization.UpdateOrganization();
}
catch (Exception ex)
{
EventHandler<FailureEventArgs> h = Failure;
FailureEventArgs args = new FailureEventArgs { ErrorMessage = ex.ToString() };
h?.Invoke(this, args);
}
}, () =>
{
return !HasErrors && !Organization.HasErrors && !Organization.Address.HasErrors;
});
}
}
// More bits...
让我犯难的是这一块。
return !HasErrors && !Organization.HasErrors && !Organization.Address.HasErrors;
如果我简单地用这个..:
return !HasErrors
...它并没有接收到子属性的错误。 我到底漏了什么?