RoutedCommand.Execute(Object, IInputElement) Method

Definition

对当前命令目标执行 RoutedCommandExecutes the RoutedCommand on the current command target.

public:
 void Execute(System::Object ^ parameter, System::Windows::IInputElement ^ target);
public void Execute (object parameter, System.Windows.IInputElement target);
[System.Security.SecurityCritical]
public void Execute (object parameter, System.Windows.IInputElement target);
member this.Execute : obj * System.Windows.IInputElement -> unit
Public Sub Execute (parameter As Object, target As IInputElement)

Parameters

parameter
Object

要传递到处理程序的用户定义的参数。User defined parameter to be passed to the handler.

target
IInputElement

要在其中查找命令处理程序的元素。Element at which to begin looking for command handlers.

Attributes

Exceptions

Examples

下面的示例来自 ICommandSource 示例的自定义实现。The following example is from a custom implementation of ICommandSource sample.

在此示例中 this.CommandICommandSource上的命令属性。this.Command in this example is the Command property on the ICommandSource. 如果此命令不为 null,则该命令将强制转换为 RoutedCommandIf the command is not null, the command is cast to a RoutedCommand. 如果是 RoutedCommand,则 Execute 方法称为传递 CommandTargetCommandParameterIf it is a RoutedCommand, then the Execute method is called passing the CommandTarget and the CommandParameter. 如果命令不是 RoutedCommand,则会将其强制转换为 ICommandExecute 方法称为传递 CommandParameterIf the command is not a RoutedCommand, it is cast to an ICommand and the Execute method is called passing the CommandParameter.

// If Command is defined, moving the slider will invoke the command;
// Otherwise, the slider will behave normally.
protected override void OnValueChanged(double oldValue, double newValue)
{
    base.OnValueChanged(oldValue, newValue);

    if (this.Command != null)
    {
        RoutedCommand command = Command as RoutedCommand;

        if (command != null)
        {
            command.Execute(CommandParameter, CommandTarget);
        }
        else
        {
            ((ICommand)Command).Execute(CommandParameter);
        }
    }
}
' If Command is defined, moving the slider will invoke the command;
' Otherwise, the slider will behave normally.
Protected Overrides Sub OnValueChanged(ByVal oldValue As Double, ByVal newValue As Double)
    MyBase.OnValueChanged(oldValue, newValue)

    If Me.Command IsNot Nothing Then
        Dim command As RoutedCommand = TryCast(Me.Command, RoutedCommand)

        If command IsNot Nothing Then
            command.Execute(CommandParameter, CommandTarget)
        Else
            CType(Me.Command, ICommand).Execute(CommandParameter)
        End If
    End If
End Sub

Remarks

执行 RoutedCommand 的实际逻辑不包含在 Execute 方法中。The actual logic that executes the RoutedCommand is not contained in the Execute methods. Execute 引发 PreviewExecutedExecuted 事件,这些事件通过元素树进行隧道和冒泡,查找具有 CommandBinding的对象。Execute raises the PreviewExecuted and Executed events, which tunnel and bubble through the element tree looking for an object with a CommandBinding. 如果找到该 RoutedCommandCommandBinding,则将调用附加到 CommandBindingExecutedRoutedEventHandlerIf a CommandBinding for that RoutedCommand is found, then the ExecutedRoutedEventHandler attached to CommandBinding is called. 这些处理程序提供执行 RoutedCommand的编程逻辑。These handlers supply the programming logic that performs the RoutedCommand.

CommandTarget上引发 PreviewExecutedExecuted 事件。The PreviewExecuted and Executed events are raised on the CommandTarget. 如果未对 ICommandSource设置 CommandTarget,将在具有键盘焦点的元素上引发 PreviewExecutedExecuted 事件。If the CommandTarget is not set on the ICommandSource, the PreviewExecuted and Executed events are raised on the element with keyboard focus.

Applies to