其他分享
首页 > 其他分享> > 自定义CommandHandler

自定义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();
    }
}

标签:canexecute,自定义,withparam,CommandHandler,Action,parameter,public
来源: https://www.cnblogs.com/jizhiqiliao/p/15766636.html