RoutedCommand.Execute(Object, IInputElement) 方法

定義

在目前的命令目標上執行 RoutedCommand

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

參數

parameter
Object

要傳遞至處理常式的使用者定義參數。

target
IInputElement

要在該處開始尋找命令處理常式的項目。

屬性

例外狀況

範例

下列範例來自範例的自定義實作 ICommandSource

this.Command 在此範例中,是 上的 ICommandSourceCommand 屬性。 如果命令不是 Null,命令會轉換成 RoutedCommandRoutedCommand如果是 ,則會Execute呼叫 方法,CommandTarget並傳遞 和 CommandParameter。 如果指令不是 RoutedCommand,則會轉換成 ICommand ,並 Execute 呼叫 方法傳遞 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

備註

執行 RoutedCommand 的實際邏輯不包含在方法中 ExecuteExecutePreviewExecuted會引發和 Executed 事件,其會透過專案樹狀結構建立通道和泡泡,尋找具有CommandBinding的物件。 CommandBinding如果找到的 RoutedCommand ,則會ExecutedRoutedEventHandler呼叫附加至 CommandBinding 的 。 這些處理程式提供執行 RoutedCommand的程式設計邏輯。

PreviewExecutedExecuted 事件會在 CommandTarget上引發。 CommandTarget如果未在 上ICommandSource設定 ,則會PreviewExecuted在具有鍵盤焦點的 元素上引發 和 Executed 事件。

適用於