RoutedCommand.Execute(Object, IInputElement) Método

Definição

Executa o RoutedCommand no alvo de comando atual.

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)

Parâmetros

parameter
Object

Parâmetro definido pelo utilizador para ser passado ao handler.

target
IInputElement

Elemento para começar a procurar controladores de comando.

Atributos

Exceções

Exemplos

O exemplo seguinte é de uma implementação personalizada de ICommandSource exemplo.

this.Command neste exemplo é a propriedade Command na ICommandSource. Se o comando não for nulo, o comando é lançado para um RoutedCommand. Se for um RoutedCommand, então o Execute método chama-se passar o CommandTarget e o CommandParameter. Se o comando não for um RoutedCommand, ele é lançado para um ICommand e o Execute método é chamado passar o 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

Observações

A lógica real que executa o RoutedCommand não está contida nos Execute métodos. Execute aumenta os PreviewExecuted eventos e, Executed que tunelam e bolham através da árvore de elementos à procura de um objeto com um CommandBinding. Se for encontrado um CommandBinding para isso RoutedCommand , então o ExecutedRoutedEventHandler ligado a CommandBinding é chamado. Estes manipuladores fornecem a lógica de programação que executa o RoutedCommand.

Os PreviewExecuted eventos e Executed são levantados no CommandTarget. Se o CommandTarget não estiver definido no ICommandSource, os PreviewExecuted eventos e Executed são elevados no elemento com foco do teclado.

Aplica-se a