ExecutedRoutedEventHandler 委托

定义

表示将处理 ExecutedPreviewExecuted 路由事件以及相关附加事件的方法。

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

附加事件处理程序的对象。

e
ExecutedRoutedEventArgs

事件数据。

示例

本示例创建一个 ExecutedRoutedEventHandler 在执行命令时在目标上设置视觉属性。 该示例还包括 CanExecuteRoutedEventHandler 同一命令。

// 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

注解

此委托包含 a RoutedCommand. 的实现逻辑。 将实现逻辑与命令分离,允许从不同的源和类型调用该命令,并启用命令逻辑的集中化。

此委托还用于 CommandManager.ExecutedCommandManager.PreviewExecuted用于在实现大部分命令基础结构的类上 CommandManager 附加事件。 但大多数实际处理程序将处理 Executed 特定 CommandBinding事件,而不是在 CommandManager 级别工作。

ExecutedRoutedEventArgs以下属性中,为从执行的命令生成的路由事件编写事件处理程序时,通常会关注以下属性:

  • Source 报告执行命令的目标。 执行命令后,还可以将路由事件术语中更普遍地 Source 视为引发路由事件的对象。

  • Command 报告执行的命令。 如果使用命令绑定,并且编写可能处理多个命令的处理程序,则此属性非常有用。

  • Parameter 报告执行命令传递的任何特定于命令的参数。 并非所有命令都使用或需要特定于命令的参数。

  • Handled 报告由执行命令生成的路由事件是否已由路由中的其他元素处理。 对于路由事件处理程序,建议在处理要设置为Handledtrue的事件时让处理程序执行有意义的工作。 这可以防止事件的典型处理程序在路由中再次处理事件。 有关处理路由事件的详细信息,请参阅将 路由事件标记为已处理,以及类处理

此委托还表示用于实现大部分命令基础结构的类上的CommandManager附加事件的处理程序CommandManager.ExecutedCommandManager.PreviewExecutedEvent处理程序。 但大多数实际处理程序将处理 Executed 特定 CommandBinding事件,而不是在 CommandManager 级别工作。

有关命令的详细信息,请参阅 命令概述

扩展方法

名称 说明
GetMethodInfo(Delegate)

获取一个对象,该对象表示由指定委托表示的方法。

适用于

另请参阅