如何编程地发射命令

时间:2022-08-25 23:36:09

i have an ICommand that I want to fire (make the execute go) from code; how do I do this?

我有一个ICommand,我想从代码中触发(执行执行);我该怎么做呢?

4 个解决方案

#1


24  

Try calling the Execute method.

尝试调用Execute方法。

#2


7  

Assuming there is someCommand with commandArgs:

假设有一些指挥官:

if (someCommand.CanExecute(commandArgs))
{
    someCommand.Execute(commandArgs);
}

#3


4  

If you're using RoutedUICommand's Execute and CanExecute, be sure to pass in a valid target so that the correct CommandBinding can be found.

如果您正在使用RoutedUICommand的Execute和CanExecute,请确保传入一个有效的目标,以便找到正确的CommandBinding。

Also, if your command's handlers do not modify View objects directly, consider using Kent Boogaart's DelegateCommand. Using delegate commands will move the business logic to the ViewModel, which is nice, and they're especially handy if you need execute commands directly from code and you don't have access to the View (or a View object from which you can bubble to your CommandBindings).

另外,如果命令的处理程序不直接修改视图对象,请考虑使用Kent Boogaart的委派。使用委托命令将业务逻辑移动到ViewModel,这很好,如果您需要直接从代码执行命令,并且您无法访问视图(或者可以从视图对象冒泡到CommandBindings),那么使用委托命令将非常方便。

#4


0  

Remember, if you need call another event of internal components, like a click button, see the methods startin with Perform (like PerformClick of a button).

请记住,如果您需要调用另一个内部组件事件,如单击按钮,则可以看到带有执行的startin方法(比如按下按钮的PerformClick)。

Another answers are okay, use Execute only...

另一个答案是可以的,只使用Execute…

#1


24  

Try calling the Execute method.

尝试调用Execute方法。

#2


7  

Assuming there is someCommand with commandArgs:

假设有一些指挥官:

if (someCommand.CanExecute(commandArgs))
{
    someCommand.Execute(commandArgs);
}

#3


4  

If you're using RoutedUICommand's Execute and CanExecute, be sure to pass in a valid target so that the correct CommandBinding can be found.

如果您正在使用RoutedUICommand的Execute和CanExecute,请确保传入一个有效的目标,以便找到正确的CommandBinding。

Also, if your command's handlers do not modify View objects directly, consider using Kent Boogaart's DelegateCommand. Using delegate commands will move the business logic to the ViewModel, which is nice, and they're especially handy if you need execute commands directly from code and you don't have access to the View (or a View object from which you can bubble to your CommandBindings).

另外,如果命令的处理程序不直接修改视图对象,请考虑使用Kent Boogaart的委派。使用委托命令将业务逻辑移动到ViewModel,这很好,如果您需要直接从代码执行命令,并且您无法访问视图(或者可以从视图对象冒泡到CommandBindings),那么使用委托命令将非常方便。

#4


0  

Remember, if you need call another event of internal components, like a click button, see the methods startin with Perform (like PerformClick of a button).

请记住,如果您需要调用另一个内部组件事件,如单击按钮,则可以看到带有执行的startin方法(比如按下按钮的PerformClick)。

Another answers are okay, use Execute only...

另一个答案是可以的,只使用Execute…