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);
[<System.Security.SecurityCritical>]
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 屬性。 若指令非空,則將該指令鑄造成 RoutedCommand。 若為 RoutedCommand,則 Execute 該方法稱為通過 CommandTargetCommandParameter與 。 若指令不是 , RoutedCommand則將 投射至 , ICommandExecute 方法稱為傳遞 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 實際邏輯並不包含在方法中 ExecuteExecute 會提升 和 PreviewExecutedExecuted 事件,這些事件會在元素樹中進行隧道和氣泡,尋找具有 的 CommandBinding物件。 若找到 a CommandBinding 對該, RoutedCommandExecutedRoutedEventHandler 附加 為 CommandBinding 。 這些處理器提供執行 RoutedCommand.

PreviewExecutedExecuted事件在 上被提出CommandTarget。 若 未 CommandTarget 設定在 ICommandSourcePreviewExecuted 則 與 Executed 事件會在帶有鍵盤焦點的元素上被觸發。

適用於