ExecutedRoutedEventHandler 代理人

定義

代表能處理 ExecutedPreviewExecuted 路由事件,以及相關附加事件的方法。Represents the method that will handle the Executed and PreviewExecuted routed events, as well as related attached events.

public delegate void ExecutedRoutedEventHandler(System::Object ^ sender, ExecutedRoutedEventArgs ^ e);
public delegate void ExecutedRoutedEventHandler(object sender, ExecutedRoutedEventArgs e);
type ExecutedRoutedEventHandler = delegate of obj * ExecutedRoutedEventArgs -> unit
Public Delegate Sub ExecutedRoutedEventHandler(sender As Object, e As ExecutedRoutedEventArgs)

參數

sender
Object

附加事件處理常式的物件。The object where the event handler is attached.

e
ExecutedRoutedEventArgs

事件資料。The event data.

繼承
ExecutedRoutedEventHandler

範例

這個範例會建立ExecutedRoutedEventHandler , 在執行命令時, 設定目標上的視覺化屬性。This example creates an ExecutedRoutedEventHandler that sets a visual property on the target when the command is executed. 此範例也包含CanExecuteRoutedEventHandler相同命令的。The example also includes the CanExecuteRoutedEventHandler for the same command.

// ExecutedRoutedEventHandler for the custom color command.
private void ColorCmdExecuted(object sender, ExecutedRoutedEventArgs e)
{
    Panel target = e.Source as Panel;
    if (target != null)
    {
        if (target.Background == Brushes.AliceBlue)
        {
            target.Background = Brushes.LemonChiffon;
        }
        else
        {
            target.Background = Brushes.AliceBlue;
        }

    }
}

// CanExecuteRoutedEventHandler for the custom color command.
private void ColorCmdCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
    if (e.Source is Panel)
    {
        e.CanExecute = true;
    }
    else
    {
        e.CanExecute = false;
    }
}
' ExecutedRoutedEventHandler for the custom color command.
Private Sub ColorCmdExecuted(ByVal sender As Object, ByVal e As ExecutedRoutedEventArgs)
    Dim target As Panel = TryCast(e.Source, Panel)
    If target IsNot Nothing Then
        If target.Background Is Brushes.AliceBlue Then
            target.Background = Brushes.LemonChiffon
        Else
            target.Background = Brushes.AliceBlue
        End If

    End If
End Sub

' CanExecuteRoutedEventHandler for the custom color command.
Private Sub ColorCmdCanExecute(ByVal sender As Object, ByVal e As CanExecuteRoutedEventArgs)
    If TypeOf e.Source Is Panel Then
        e.CanExecute = True
    Else
        e.CanExecute = False
    End If
End Sub

備註

這個委派包含的執行邏輯RoutedCommandThis delegate contains the implementation logic for a RoutedCommand. 將執行邏輯與命令隔開, 可讓您從不同的來源和類型叫用命令, 並啟用命令邏輯的集中化。Separating the implementation logic from the command allows the command to be invoked from different sources and types, and enables the centralization of command logic.

這個委派也會用於CommandManager.ExecutedCommandManager.PreviewExecuted, 這些CommandManager是在執行許多命令基礎結構的類別上附加事件。This delegate is also used for CommandManager.Executed and CommandManager.PreviewExecuted, which are attached events on the CommandManager class that implements much of the commanding infrastructure. 但是, 大部分的Executed實際處理常式將會處理特定CommandBinding的事件, CommandManager而不是在層級工作。But most practical handlers will handle the Executed events from a particular CommandBinding, rather than work at the CommandManager level.

ExecutedRoutedEventArgs中, 當您為已執行的命令所產生的路由事件撰寫事件處理常式時, 通常會對下列屬性有意義:Within ExecutedRoutedEventArgs, the following properties are generally of interest when you write an event handler for a routed event that results from an executed command:

  • Source報告執行命令的目標。Source reports the target where the command was executed. 執行命令之後, 您也Source可以將更常見的路由事件詞彙視為引發路由事件的物件。Once the command is executed, you can also think of the Source more generally in routed event terms, as the object that raised a routed event.

  • Command報告執行的命令。Command reports the command that executed. 如果您使用命令系結, 以及撰寫可能處理多個命令的處理常式, 這個屬性就很有用。This property is useful if you use command bindings and if you write handlers that potentially handle multiple commands.

  • Parameter報告執行中命令所傳遞的任何命令特定參數。Parameter reports any command-specific parameters that were passed by the executing command. 並非所有命令都使用或預期命令特有的參數。Not all commands use or expect command-specific parameters.

  • Handled報告執行的命令所產生的路由事件是否已經由路由中的不同元素處理。Handled reports whether the routed event that resulted from the executed command was already handled by a different element along the route. 對於路由事件處理常式, 建議的做法是在處理要設定Handledtrue的事件時, 執行有意義的工作的處理常式。For routed event handlers, it is a recommended practice to have handlers that do meaningful work when handling the event to set Handled to true. 這可防止事件的一般處理常式沿著路由進一步進一步處理事件。This prevents typical handlers for the event from handling the event again further along the route. 如需處理路由事件的詳細資訊, 請參閱將路由事件標示為已處理以及類別處理For more information on handling routed events, see Marking Routed Events as Handled, and Class Handling.

這個委派也代表CommandManager.ExecutedCommandManager.PreviewExecutedEvent的處理常式, 這些CommandManager是在執行許多命令基礎結構的類別上附加事件。This delegate also represents handlers for CommandManager.Executed and CommandManager.PreviewExecutedEvent, which are attached events on the CommandManager class that implements much of the commanding infrastructure. 但是, 大部分的Executed實際處理常式將會處理特定CommandBinding的事件, CommandManager而不是在層級工作。But most practical handlers will handle the Executed events from a particular CommandBinding, rather than work at the CommandManager level.

如需命令的詳細資訊,請參閱命令概觀For more information on commanding, see Commanding Overview.

擴充方法

GetMethodInfo(Delegate)

取得表示特定委派所代表之方法的物件。Gets an object that represents the method represented by the specified delegate.

適用於

另請參閱