RoutedCommand.Execute(Object, IInputElement) Método

Definição

Executa o destino de RoutedCommand 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 usuário a ser passado para o manipulador.

target
IInputElement

Elemento no qual começar a procurar manipuladores de comando.

Atributos

Exceções

Exemplos

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

this.Command neste exemplo está a propriedade Command na ICommandSource. Se o comando não for nulo, o comando será convertido em um RoutedCommand. Se for umRoutedCommand, o Execute método será chamado passando o CommandTarget .CommandParameter Se o comando não for um RoutedCommand, ele será convertido em um ICommand e o Execute método será chamado passando 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

Comentários

A lógica real que executa não RoutedCommand está contida nos Execute métodos. Execute gera o PreviewExecuted e Executed eventos, que túnel e bolha através da árvore de elementos procurando um objeto com um CommandBinding. Se um CommandBinding para isso RoutedCommand for encontrado, o ExecutedRoutedEventHandler anexado CommandBinding será chamado. Esses manipuladores fornecem a lógica de programação que executa o RoutedCommand.

O PreviewExecuted e Executed os eventos são gerados na CommandTarget. Se não CommandTarget estiver definido no ICommandSource, os eventos e os PreviewExecutedExecuted eventos serão gerados no elemento com foco no teclado.

Aplica-se a