使用英语阅读

通过


如何:为路由事件添加类处理

路由事件可由路由中的任何给定节点上的类处理程序或实例处理程序处理。 首先调用类处理程序,类实现可以使用这些处理程序来禁止实例处理中的事件,或在基类拥有的事件上引入其他事件特定行为。 此示例演示了实现类处理程序的两种密切相关的技术。

此示例使用基于 Canvas 面板的自定义类。 应用程序的基本前提是,自定义类在其子元素上引入行为,包括在调用子元素类或其上的任何实例处理程序之前拦截任何鼠标左键单击操作并将其标记为“已处理”。

UIElement 类公开了一个虚拟方法,该方法通过简单地重写事件来启用对 PreviewMouseLeftButtonDown 事件的类处理。 如果此类虚拟方法在类层次结构中的某个位置可用,则这是实现类处理的最简单方法。 以下代码显示了派生自 Canvas的“MyEditContainer”中的 OnPreviewMouseLeftButtonDown 实现。 该实现将事件标记为在参数中处理,然后添加一些代码来为源元素提供基本的可见更改。

protected override void OnPreviewMouseRightButtonDown(System.Windows.Input.MouseButtonEventArgs e)
{
    e.Handled = true; //suppress the click event and other leftmousebuttondown responders
    MyEditContainer ec = (MyEditContainer)e.Source;
    if (ec.EditState)
    { ec.EditState = false; }
    else
    { ec.EditState = true; }
    base.OnPreviewMouseRightButtonDown(e);
}
Protected Overrides Sub OnPreviewMouseRightButtonDown(ByVal e As System.Windows.Input.MouseButtonEventArgs)
    e.Handled = True 'suppress the click event and other leftmousebuttondown responders
    Dim ec As MyEditContainer = CType(e.Source, MyEditContainer)
    If ec.EditState Then
        ec.EditState = False
    Else
        ec.EditState = True
    End If
    MyBase.OnPreviewMouseRightButtonDown(e)
End Sub

如果基类或该特定方法上没有可用的虚拟方法,则可以使用 EventManager 类的实用方法 RegisterClassHandler 直接添加类处理。 此方法只能在添加类处理的类的静态初始化中调用。 此示例为 PreviewMouseLeftButtonDown 添加另一个处理程序,在本例中,已注册的类是自定义类。 相比之下,使用虚拟时,注册的类实际上是 UIElement 基类。 在基类和子类各自注册类处理的情况下,将首先调用子类处理程序。 应用程序中的行为是,首先此处理程序会显示其消息框,然后会显示虚拟方法处理程序中的视觉更改。

static MyEditContainer()
{
  EventManager.RegisterClassHandler(typeof(MyEditContainer), PreviewMouseRightButtonDownEvent, new RoutedEventHandler(LocalOnMouseRightButtonDown));
}
internal static void LocalOnMouseRightButtonDown(object sender, RoutedEventArgs e)
{
  MessageBox.Show("this is invoked before the On* class handler on UIElement");
  //e.Handled = true; //uncommenting this would cause ONLY the subclass' class handler to respond
}
Shared Sub New()
    EventManager.RegisterClassHandler(GetType(MyEditContainer), PreviewMouseRightButtonDownEvent, New RoutedEventHandler(AddressOf LocalOnMouseRightButtonDown))
End Sub
Friend Shared Sub LocalOnMouseRightButtonDown(ByVal sender As Object, ByVal e As RoutedEventArgs)
    MessageBox.Show("this is invoked before the On* class handler on UIElement")
    'e.Handled = True //uncommenting this would cause ONLY the subclass' class handler to respond
End Sub

另请参阅


其他资源

培训

模块

管理类实现 - Training

了解如何使用高级技术(如静态类、分部类和对象初始值设定项)实现类,这些技术可以提高代码的可读性、可维护性和组织性。