Bagikan melalui


RoutedCommand.Execute(Object, IInputElement) Metode

Definisi

RoutedCommand Menjalankan pada target perintah saat ini.

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

parameter
Object

Parameter yang ditentukan pengguna untuk diteruskan ke handler.

target
IInputElement

Elemen untuk mulai mencari penangan perintah.

Atribut

Pengecualian

Contoh

Contoh berikut berasal dari implementasi ICommandSource kustom sampel.

this.Command dalam contoh ini adalah properti Perintah pada ICommandSource. Jika perintah tidak null, perintah akan dilemparkan ke RoutedCommand. Jika itu adalah RoutedCommand, maka Execute metode ini disebut melewati CommandTarget dan CommandParameter. Jika perintah bukan RoutedCommand, perintah ditransmisikan ke dan ICommandExecute metode disebut meneruskan 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

Keterangan

Logika aktual yang menjalankan RoutedCommand tidak terkandung dalam Execute metode . ExecutePreviewExecuted meningkatkan peristiwa dan Executed , yang terowongan dan gelembung melalui pohon elemen mencari objek dengan CommandBinding. CommandBinding Jika untuk itu RoutedCommand ditemukan, maka yang ExecutedRoutedEventHandler terlampir CommandBinding akan dipanggil. Handler ini menyediakan logika pemrograman yang melakukan RoutedCommand.

Peristiwa PreviewExecuted dan Executed dimunculkan pada CommandTarget. CommandTarget Jika tidak diatur pada ICommandSource, PreviewExecuted peristiwa dan Executed dinaikkan pada elemen dengan fokus keyboard.

Berlaku untuk