RoutedCommand.Execute(Object, IInputElement) Metodo

Definizione

Esegue RoutedCommand in corrispondenza della destinazione corrente del comando.

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)

Parametri

parameter
Object

Parametro definito dall'utente da passare al gestore.

target
IInputElement

Elemento in cui effettuare la ricerca dei gestori di comando.

Attributi

Eccezioni

target non è un oggetto UIElementContentElement.

Esempio

L'esempio seguente è tratto da un'implementazione personalizzata di ICommandSource esempio.

this.Command in questo esempio è la proprietà Command dell'oggetto ICommandSource. Se il comando non è Null, viene eseguito il cast del comando a .RoutedCommand Se è un RoutedCommandoggetto , il Execute metodo viene chiamato passando e CommandTarget .CommandParameter Se il comando non è un RoutedCommandoggetto , viene eseguito il cast a un ICommand oggetto e il Execute metodo viene chiamato passando l'oggetto 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

Commenti

La logica effettiva che esegue l'oggetto RoutedCommand non è contenuta nei Execute metodi . Execute genera gli PreviewExecuted eventi e Executed , che eseguono il tunneling e la bolla attraverso l'albero degli elementi alla ricerca di un oggetto con un oggetto CommandBinding. Se viene trovato un CommandBinding oggetto per , RoutedCommand viene chiamato l'oggetto ExecutedRoutedEventHandler a CommandBinding cui è associato. Questi gestori forniscono la logica di programmazione che esegue .RoutedCommand

Gli PreviewExecuted eventi e Executed vengono generati nell'oggetto CommandTarget. Se non CommandTarget è impostato su ICommandSource, gli PreviewExecuted eventi e Executed vengono generati sull'elemento con lo stato attivo della tastiera.

Si applica a