如果您实现自己的 DelegateCommand,您可以拥有接收无参数操作的构造函数,如以下代码所示:
public DelegateCommand(Action<object> execute, Func<object, bool> canExecute)
{
if (execute == null)
throw new ArgumentNullException("execute", "execute cannot be null");
if (canExecute == null)
throw new ArgumentNullException("canExecute", "canExecute cannot be null");
_execute = execute;
_canExecute = canExecute;
}
public DelegateCommand(Action<object> execute)
: this(execute, (o) => true)
{
}
public DelegateCommand(Action execute)
: this((o) => execute())
{
}
使用最后一个构造函数重载,您可以做到
var someCommand = new DelegateCommand(SomeMethod);
其中Method是无参数的