RoutedCommand.Execute(Object, IInputElement) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
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
從哪裡開始尋找指令處理者。
- 屬性
例外狀況
target 不是 UIElement 或 ContentElement。
範例
以下範例來自一個自訂的範例實作 ICommandSource 。
this.Command 此範例中為 的 ICommandSourceCommand 屬性。 若指令非空,則將該指令鑄造成 RoutedCommand。 若為 RoutedCommand,則 Execute 該方法稱為通過 CommandTargetCommandParameter與 。 若指令不是 , 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 實際邏輯並不包含在方法中 Execute 。 Execute 會提升 和 PreviewExecutedExecuted 事件,這些事件會在元素樹中進行隧道和氣泡,尋找具有 的 CommandBinding物件。 若找到 a CommandBinding 對該, RoutedCommand 則 ExecutedRoutedEventHandler 附加 為 CommandBinding 。 這些處理器提供執行 RoutedCommand.
與PreviewExecutedExecuted事件在 上被提出CommandTarget。 若 未 CommandTarget 設定在 ICommandSource, PreviewExecuted 則 與 Executed 事件會在帶有鍵盤焦點的元素上被觸發。