C# MVVM,ICommand的简单实现

Model部分
实现INotifyPropertyChanged接口,使其能够发出某一属性值发生改变的通知

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

namespace MVVMTest.Model
{
    public class PersonModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public PersonModel()
        {
            Name = "name";
            Age = "20";
            Sex = "男";
        }

        public string name;
        public string Name
        {
            get {   return name;}
            set
            {
                name = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(Name));  
            }
        }

        public string age;
        public string Age
        {
            get { return age; }
            set
            {
                age = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Age"));
            }
        }

        public string sex;
        public string Sex
        {
            get { return sex; }
            set
            {
                sex = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Sex"));
            }
        }
    }
}

ViewModel部分:
首先实现Icommand接口,包含一个Action和Func委托。

    public class DeleteCommand : ICommand
    {
        public event EventHandler CanExecuteChanged;

        public Action _execute = null;
        public Func _canExecute = null ;

        
        public DeleteCommand(Action execute ):this(execute,null)
        {
        }

        public DeleteCommand(Action execute, Func canExecute)
        {
            if (canExecute != null)
            {
                this._canExecute = new Func(canExecute);
            }
            if (execute != null)
            {
                this._execute = new Action(execute);
            }
        }
        
        public void RaiseCanExceuteChanged()
        {
            if (CanExecuteChanged != null)
            {
                CanExecuteChanged(this, EventArgs.Empty);
            }
        }

        public bool CanExecute(object parameter)
        {
            if (this._canExecute == null)
            {
                return true;
            }
            return _canExecute((T)(object)(parameter));
        }

        public void Execute(object parameter)
        {
            this._execute((T)(object)(parameter));
        }
    }
}

PersonViewModel类作为数据源,其中包含Model实例和一个DeleteCommand命令;

public class PersonViewModel
    {

    //    public delegate void Fun(PersonModel person);

        public PersonModel Model{ set; get; }
        public DeleteCommand deleteCommand { set; get; }

        public PersonViewModel()
        {
            Model = new PersonModel();
            deleteCommand = new DeleteCommand( ClearData ,IsPersonNotEmpty);
        }

        public void ClearData(PersonModel person)
        {
        //    PersonModel personModel = person as PersonModel;
            Model.Age = "";
            Model.Name = "";
            Model.Sex = "";
        }

        public bool IsPersonNotEmpty(PersonModel person)
        {
        //    PersonModel personModel = person as PersonModel;
        //    if (string.IsNullOrEmpty(personModel.Name) && string.IsNullOrEmpty(personModel.Age) && string.IsNullOrEmpty(personModel.Sex))
        //        return false;
            return true;
        }
    }

View部分


    
        
            
                
                
                

程序运行:
C# MVVM,ICommand的简单实现_第1张图片

点击Clear按钮
C# MVVM,ICommand的简单实现_第2张图片

你可能感兴趣的:(learn)