RoutedCommand.Execute(Object, IInputElement) 方法


在目前的命令目標上執行 RoutedCommandExecutes the RoutedCommand on the current command target.

 void Execute(System::Object ^ parameter, System::Windows::IInputElement ^ target);
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)



要傳遞至處理常式的使用者定義參數。User defined parameter to be passed to the handler.


要在該處開始尋找命令處理常式的項目。Element at which to begin looking for command handlers.



target 不是 UIElement 也不是 ContentElementtarget is not a UIElement or ContentElement.


下列範例來自 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,它會轉換成 ICommand,而 Execute 方法則稱為傳遞 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);
' 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)
            CType(Me.Command, ICommand).Execute(CommandParameter)
        End If
    End If
End Sub


執行 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.

PreviewExecutedExecuted 事件會在 CommandTarget上引發。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.