1.DelegateCommand.cs文章来源:https://www.toymoban.com/news/detail-569262.html
public class DelegateCommand : ICommand
{
public Action<object> ExecuteAction { get; set; }
public Func<object, bool> CanExecuteFunc { get; set; }
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 DelegateCommand() { }
public DelegateCommand(Action<object> execute):this(execute,null) { }
public DelegateCommand(Action<object> execute, Func<object,bool> canexcute)
{
this.CanExecuteFunc = canexcute;
this.ExecuteAction = execute;
}
}
NotificationObject.cs文章来源地址https://www.toymoban.com/news/detail-569262.html
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));
}
}
}
}
到了这里,关于wpf自定义Mvvm框架的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!