public class SelfCheckCommand : ICommand
{
public SelfCheckCommand()
{
}
public SelfCheckCommand(Action<object> execAction, Func<object, bool> canExecte)
{
ExecuteAction = execAction;
CanExecuteFunc = canExecte;
}
private bool _canExecuteCache;
public bool CanExecute(object parameter)
{
if (CanExecuteFunc == null)
{
return true;
}
bool result = CanExecuteFunc(parameter);
if (result != _canExecuteCache)
{
_canExecuteCache = result;
}
return result;
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public void Execute(object parameter)
{
if (ExecuteAction != null)
{
this.ExecuteAction(parameter);
}
}
public Action<object> ExecuteAction { get; set; }
public Func<object, bool> CanExecuteFunc { get; set; }
}
本文详细介绍了如何创建和使用自定义命令,包括构造函数、执行动作和判断执行条件等功能,适用于需要灵活控制命令行为的应用场景。
4万+

被折叠的 条评论
为什么被折叠?



