我有一个基本调用BaseViewModel,它由一个IList集合组成,如下所示:
public class BaseViewModel
{
private IList<BrokenRule> _brokenRules = new List<BrokenRule>();
public IList<BrokenRule> BrokenRules
{
get { return _brokenRules; }
set { _brokenRules = value; }
}
public void ShowBrokenRules()
{
_brokenRulePresenter.Present(_brokenRules);
}
}
我的视图模型UserEditViewModel继承自BaseViewModel。验证引擎验证模型并填充损坏的规则。当我从具体类(UserEditViewModel)调用ShowBrokenRules时,破坏的规则集合不包含任何内容。
UserEditViewModel保存方法:
private void Save(UserEditViewModel userEditViewModel)
{
bool isValid = ValidationEngine.Validate(this.UserViewModel); //
if (!isValid)
{
ShowBrokenRules();
}
}
我认为原因是在ShowBrokenRules方法中_brokenRules引用了基类实现。我是对的吗?
UserEditViewModel类:
public class UserEditViewModel : BaseViewModel
{
private UserViewModel _userViewModel;
private void Save(UserEditViewModel userEditViewModel)
{
bool isValid = ValidationEngine.Validate(this.UserViewModel); // The UserViewModel.brokenrules is populated not the UserEditViewModel.brokenrules maybe that is the problem.
if (!isValid)
{
ShowBrokenRules();
}
}
答案 0 :(得分:1)
您正在ValidationEngine.Validate()
实例的UserViewModel
成员属性上调用UserEditViewModel
而不是实例本身。这将导致函数返回false
,因为验证失败,但BrokenRules是在成员对象上设置的,而不是对象本身。因此,当您调用ShowBrokenRules()
时,它会显示父对象的破坏规则,而不是传递给ValidationEngine.Validate()
方法的对象。