MVVM - ICommand and INotifyPropertyChanged query

本文关键字:INotifyPropertyChanged query and ICommand MVVM | 更新日期: 2023-09-27 18:06:38

全部,

刚启动w/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
}

MVVM - ICommand and INotifyPropertyChanged query

  1. 在我有更多经验的框架平台中,ICommand是用一个名为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()
        }
    //...
    

    }