2014-07-16 50 views
0

我有一個基礎的呼叫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

是你的'UserEditViewModel'以某種方式覆蓋你的'ShowBrokenRules'方法? –

+1

你可以顯示UserEditViewModel類嗎? –

+0

我會推薦將'BrokenRules'公共訪問器傳遞給'Present'方法,而不是直接公開'_brokenRules'實現。 –

回答

1

您在UserEditViewModel實例的UserViewModel成員屬性,而不是實例本身調用ValidationEngine.Validate()。這將導致函數返回false,因爲驗證失敗,但BrokenRules設置在成員對象上,而不是對象本身。因此,當您撥打ShowBrokenRules()時,它會顯示父對象的損壞規則,而不是傳遞給ValidationEngine.Validate()方法的對象。