wpf自定义Mvvm框架

1.DelegateCommand.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace SimpleMvvmDemo.Commands
{
    class DelegateCommand : ICommand
    {

        public event EventHandler CanExecuteChanged;

        public bool CanExecute(object parameter)
        {
            // throw new NotImplementedException();
            if(this.CanExecuteFunc==null)
            {
                return true;
            }
            this.CanExecuteFunc(parameter);
            return true;
        }

        public void Execute(object parameter)
        {
            //throw new NotImplementedException();
            if(this.ExecuteAction==null)
            {
                return;
            }
            this.ExecuteAction(parameter); //命令->Execute->Execute指向的方法
        }

        public Action<object> ExecuteAction { get; set; }
        public Func<object, bool> CanExecuteFunc { get; set; }
    }
}

2。NotificationObject.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SimpleMvvmDemo.viewmodel
{
    //viewmodel的基类
    class NotificationObject : INotifyPropertyChanged 
    {
        public event PropertyChangedEventHandler PropertyChanged;
        public void RaisePropertyChanged(string propertyName)
        {
            if(this.PropertyChanged!=null)
            {
                //binding监控changed
                this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName)); 
            }
        }
    }
}

相关推荐