2013-03-29 72 views
0

所有,MVVM - ICommand的和INotifyPropertyChanged的查詢

剛開始瓦特/ MVVM ......得到了交談ABT MVVM幾篇文章......我有兩個疑問..

  1. 始終INotifyPropertyChanged的和的ICommand執行會是這樣嗎?還是需要其他更改?

  2. 如果我點擊某個按鈕,需要調用一些模型的方法?我怎樣才能達到目的?

THX提前..

這家酒店實現@模型

#region INotifyPropertyChanged Members 
public event PropertyChangedEventHandler PropertyChanged; 
private void OnPropertyChanged(string propertyName) 
{ 
    if (PropertyChanged != null) 
    { 
     PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
    } 
} 
#endregion 

的ICommand - 這是實現@ VM

private ICommand mUpdater; 
public ICommand UpdateCommand 
{ 
    get 
    { 
     if (mUpdater == null) 
      mUpdater = new Updater(); 
     return mUpdater; 
    } 
    set 
    { 
     mUpdater = value; 
    } 
} 

private class Updater : ICommand 
{ 
    #region ICommand Members 
    public bool CanExecute(object parameter) 
    { 
     return true; 
    } 

    public event EventHandler CanExecuteChanged; 

    public void Execute(object parameter) 
    { 

    } 

    #endregion 
} 

回答

0
  1. framework platform with which I've had more experience ICommand是imp與一個叫做DelegateCommand的漂亮類共事。它基本上允許你在其他地方實現你的ExecuteCanExecute方法。

  2. 在您的視圖模型,你將有一個命令,然後執行該視圖模型的模型的方法:

    public class SomeViewModel : ViewModelBase<SomeModel> 
    { 
        //implemented in the base class: 
        //public Model SomeModel { get; } 
    
        internal ICommand SomeMethodThatIsReallyOnMyModel 
        { 
         get 
         { 
          return _someCommandYouHaveImplementedToDoJustThis; 
         } 
         //_someCommandYouHaveImplementedToDoJustThis.Execute: 
         //Model.SomeMethod() 
        } 
    
    //... 
    

    }