RoutedCommand.Execute(Object, IInputElement) Metoda

Definicja

Wykonuje element RoutedCommand na bieżącym obiekcie docelowym polecenia.

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)

Parametry

parameter
Object

Parametr zdefiniowany przez użytkownika, który ma zostać przekazany do procedury obsługi.

target
IInputElement

Element, na którym chcesz rozpocząć wyszukiwanie procedur obsługi poleceń.

Atrybuty

Wyjątki

target nie jest elementem UIElement lub ContentElement.

Przykłady

Poniższy przykład pochodzi z niestandardowej implementacji przykładu ICommandSource .

this.Command w tym przykładzie jest właściwość Command w elemecie ICommandSource. Jeśli polecenie nie ma wartości null, polecenie jest rzutowanie do elementu RoutedCommand. Jeśli jest to RoutedCommandelement , Execute metoda jest wywoływana przez przekazanie elementu CommandTarget i CommandParameter. Jeśli polecenie nie jest elementem RoutedCommand, jest rzutowanie do elementu ICommand , a Execute metoda jest wywoływana CommandParameterprzez przekazanie elementu .

// 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

Uwagi

Rzeczywista logika wykonywana przez RoutedCommand metodę nie jest zawarta w metodach Execute . Execute zgłasza PreviewExecuted zdarzenia i Executed , które tunelować i bąbelkowe przez drzewo elementów, wyszukując obiekt z obiektem CommandBinding. Jeśli element CommandBinding dla tego RoutedCommand elementu zostanie znaleziony, wywołana ExecutedRoutedEventHandler jest dołączona wartość CommandBinding . Te procedury obsługi dostarczają logikę programowania, która wykonuje RoutedCommandelement .

Zdarzenia PreviewExecuted i Executed są wywoływane w obiekcie CommandTarget. Jeśli element CommandTarget nie jest ustawiony na ICommandSourceobiekcie , PreviewExecuted zdarzenia i Executed są wywoływane na elemecie z fokusem klawiatury.

Dotyczy