RoutedCommand.Execute(Object, IInputElement) Metoda

Definicja

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

C#
[System.Security.SecurityCritical]
public void Execute(object parameter, System.Windows.IInputElement target);
C#
public void Execute(object parameter, System.Windows.IInputElement target);

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 .

C#
// 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);
        }
    }
}

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

Produkt Wersje
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10