RoutedCommand.Execute(Object, IInputElement) Metoda
Definicja
Ważne
Niektóre informacje odnoszą się do produktu w wersji wstępnej, który może zostać znacząco zmodyfikowany przed wydaniem. Firma Microsoft nie udziela żadnych gwarancji, jawnych lub domniemanych, w odniesieniu do informacji podanych w tym miejscu.
Wykonuje element RoutedCommand na bieżącym obiekcie docelowym polecenia.
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)
Parametry
- parameter
- Object
Parametr zdefiniowany przez użytkownika, który ma zostać przekazany do programu obsługi.
- target
- IInputElement
Element, w którym chcesz rozpocząć wyszukiwanie procedur obsługi poleceń.
- Atrybuty
Wyjątki
target nie jest elementem UIElement lub ContentElement.
Przykłady
Poniższy przykład pochodzi z niestandardowej implementacji przykładu ICommandSource .
this.Command w tym przykładzie jest właściwością Command w pliku ICommandSource. Jeśli polecenie nie ma wartości null, polecenie zostanie oddane do RoutedCommandelementu . Jeśli jest RoutedCommandto element , Execute metoda jest wywoływana, przekazując element CommandTarget i CommandParameter. Jeśli polecenie nie jest poleceniem RoutedCommand, jest rzutowanie do elementu ICommand , a Execute metoda jest wywoływana CommandParameter, przekazując element .
// 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
Uwagi
Rzeczywista logika, która wykonuje RoutedCommand element , nie jest zawarta w metodach Execute . Execute PreviewExecuted wywołuje zdarzenia i Executed , które tunelują i bąbelkami przez drzewo elementów, wyszukując obiekt z obiektem CommandBinding. CommandBinding Jeśli element dla tego RoutedCommand elementu zostanie znaleziony, wywołana ExecutedRoutedEventHandler jest wartość dołączona doCommandBinding. Te programy obsługi udostępniają logikę programowania, która wykonuje RoutedCommand.
Zdarzenia PreviewExecuted i Executed są wywoływane w obiekcie CommandTarget. Jeśli właściwość CommandTarget nie jest ustawiona na ICommandSourceobiekcie , PreviewExecuted zdarzenia i Executed są wywoływane na elemecie z fokusem klawiatury.