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

Элемент, с которого начинается поиск обработчиков команд.

Атрибуты

Исключения

target не является UIElement или ContentElement.

Примеры

В следующем примере представлена пользовательская реализация ICommandSource примера.

this.Command В этом примере используется свойство Command в объекте ICommandSource. Если команда не имеет значения NULL, команда выполняется приведение к объекту RoutedCommand. Если это RoutedCommandExecute , то метод вызывается передачей 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 , которая выполняется, не содержится в методах Execute . Executeвызывает и PreviewExecuted события, которые туннелирует и пузырьк Executed через дерево элементов ищет объект с CommandBindingобъектом. CommandBinding RoutedCommand Если для этого найдено, вызывается присоединенный ExecutedRoutedEventHandlerCommandBinding объект. Эти обработчики предоставляют логику программирования, которая выполняет .RoutedCommand

На PreviewExecuted них Executedсоздаются событияCommandTarget. Если на CommandTarget элементе не задано ICommandSourceзначение, PreviewExecutedExecuted события создаются на элементе с фокусом клавиатуры.

Применяется к