0

All,

Just started w/ MVVM... got few articles that talk abt MVVM... I have 2 queries..

  1. Always INotifyPropertyChanged and ICommand implementation will be like this? or some other changes required?

  2. If I click on some button and need to call some method of model ? How can I achive that?

Thx in advance..

This property is implemented @ model

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

ICommand -- this is implemented @ 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
}
4

1 回答 1

0
  1. 我有更多经验的框架平台中, ICommand 是通过一个名为DelegateCommand. 它基本上允许您在其他地方 实现您的Execute和方法。CanExecute

  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()
        }
    
    //...
    

    }

于 2013-03-29T22:28:17.867 回答