自定义命令
MyCommand.cs
public class MyCommand : ICommand
{private readonly Action<Object> execAction;private readonly Func<Object,bool> changedFunc;public event EventHandler? CanExecuteChanged;public MyCommand(Action<object> execAction,Func<object,bool>changeFunc){this.execAction = execAction;this.changedFunc=changeFunc;}public bool CanExecute(object? parameter){return this.changedFunc.Invoke(parameter);}public void Execute(object? parameter){// 逻辑this.execAction.Invoke(parameter);}
}
ViewMode.cs
public class ViewMode{bool isCanExec = true;// 这是属性的写法 ,命令必须是属性public ICommand MyCommand=>new MyCommand(MyAction,MyCanExec);private void MyAction(object prampartter){// 写逻辑MessageBox.Show("我被触发啦");isCanExec = false;}private bool MyCanExec(object pramparter){return isCanExec;}}