自定义CommandHandler

class CommandHandler : ICommand
{
    Action action;
    Action<object> action1;
    Predicate<object> canexecute;
    bool withparam;
    bool _canexecute;

    public CommandHandler(Action act)
    {
        action = act;
        _canexecute = true;
        withparam = false;
    }

    public CommandHandler(Action<object> act, Predicate<object> canexe)
    {
        action1 = act;
        canexecute = canexe;
        withparam = true;
    }

    public event EventHandler CanExecuteChanged;

    public bool CanExecute(object parameter)
    {
        if (withparam)
            return canexecute(parameter);
        else
            return _canexecute;
    }

    public void Execute(object parameter)
    {
        if (withparam)
            action1(parameter);
        else
            action();
    }
}
posted @ 2022-01-05 13:52  Kyle0418  阅读(138)  评论(0编辑  收藏  举报