如何:创建自定义路由事件

若要使自定义事件支持事件路由,需要 RoutedEvent 使用 RegisterRoutedEvent 方法注册。 本示例演示了创建自定义路由事件的基本原理。

示例

如下面的示例中所示,您首先 RoutedEvent 使用方法注册 RegisterRoutedEvent 。 按照约定, RoutedEvent 静态字段名称应以后缀 RoutedEvent结束。 在此示例中,事件的名称为 Tap ,事件的路由策略是 Bubble 。 注册调用后,可以为事件提供添加和删除公共语言运行时 (CLR) 事件访问器。

请注意,尽管事件在此特定示例中是通过 OnTap 虚拟方法引发的,但引发事件的方式或事件响应更改的方式取决于你的需要。

另请注意,此示例基本上实现了的整个子类 Button ; 该子类构建为单独的程序集,然后在单独的 Extensible Application Markup Language (XAML) "页上实例化为自定义类。 这是为了说明子类控件可插入到由其他控件组成的树中的概念,在这种情况下,这些控件上的自定义事件具有与任何本机 Windows Presentation Foundation (WPF) 元素相同的事件路由功能。

public class MyButtonSimple: Button
{
    // Create a custom routed event by first registering a RoutedEventID
    // This event uses the bubbling routing strategy
    public static readonly RoutedEvent TapEvent = EventManager.RegisterRoutedEvent(
        "Tap", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(MyButtonSimple));

    // Provide CLR accessors for the event
    public event RoutedEventHandler Tap
    {
            add { AddHandler(TapEvent, value); }
            remove { RemoveHandler(TapEvent, value); }
    }

    // This method raises the Tap event
    void RaiseTapEvent()
    {
            RoutedEventArgs newEventArgs = new RoutedEventArgs(MyButtonSimple.TapEvent);
            RaiseEvent(newEventArgs);
    }
    // For demonstration purposes we raise the event when the MyButtonSimple is clicked
    protected override void OnClick()
    {
        RaiseTapEvent();
    }
}
Public Class MyButtonSimple
    Inherits Button

    ' Create a custom routed event by first registering a RoutedEventID
    ' This event uses the bubbling routing strategy
    Public Shared ReadOnly TapEvent As RoutedEvent = EventManager.RegisterRoutedEvent("Tap", RoutingStrategy.Bubble, GetType(RoutedEventHandler), GetType(MyButtonSimple))

    ' Provide CLR accessors for the event
    Public Custom Event Tap As RoutedEventHandler
        AddHandler(ByVal value As RoutedEventHandler)
            Me.AddHandler(TapEvent, value)
        End AddHandler

        RemoveHandler(ByVal value As RoutedEventHandler)
            Me.RemoveHandler(TapEvent, value)
        End RemoveHandler

        RaiseEvent(ByVal sender As Object, ByVal e As RoutedEventArgs)
            Me.RaiseEvent(e)
        End RaiseEvent
    End Event

    ' This method raises the Tap event
    Private Sub RaiseTapEvent()
        Dim newEventArgs As New RoutedEventArgs(MyButtonSimple.TapEvent)
        MyBase.RaiseEvent(newEventArgs)
    End Sub

    ' For demonstration purposes we raise the event when the MyButtonSimple is clicked
    Protected Overrides Sub OnClick()
        Me.RaiseTapEvent()
    End Sub

End Class
<Window  
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:custom="clr-namespace:SDKSample;assembly=SDKSampleLibrary"
    x:Class="SDKSample.RoutedEventCustomApp"

    >
    <Window.Resources>
      <Style TargetType="{x:Type custom:MyButtonSimple}">
        <Setter Property="Height" Value="20"/>
        <Setter Property="Width" Value="250"/>
        <Setter Property="HorizontalAlignment" Value="Left"/>
        <Setter Property="Background" Value="#808080"/>
      </Style>
    </Window.Resources>
    <StackPanel Background="LightGray">
        <custom:MyButtonSimple Name="mybtnsimple" Tap="TapHandler">Click to see Tap custom event work</custom:MyButtonSimple>
    </StackPanel>
</Window>

隧道事件是以相同方式创建的,但 RoutingStrategyTunnel 在注册调用中设置为。 按照约定,WPF 中的隧道事件以单词 "Preview" 作为前缀。

若要查看浮升事件的工作原理示例,请参阅处理路由事件

另请参阅