如何:创建自定义路由事件
若要使您的自定义事件支持事件路由,需要使用 RegisterRoutedEvent 方法注册 RoutedEvent。 本示例演示创建自定义路由事件的基本原理。
示例
如下面的示例所示,首先使用 RegisterRoutedEvent 方法注册一个 RoutedEvent。 按照约定,RoutedEvent 静态字段名称应当以后缀 Event 结束。 在本示例中,事件的名称是 Tap,事件的路由策略是 Bubble。 在注册调用之后,可以为该事件提供添加和移除common language runtime (CLR) 事件访问器。
请注意,尽管该事件在本特定示例中是通过 OnTap 虚方法引发的,但您引发事件的方式或者事件响应更改的方式取决于您的需要。
还要注意,本示例主要实现 Button 的一整个子类;该子类是作为单独的程序集构建的,之后将在单独的Extensible Application Markup Language (XAML) 页上实例化为一个自定义类。 这是为了说明这样一个概念:创建子类的控件可以插入到由其他控件组成的树中,在这种情况下,这些控件上的自定义事件具有与任何固有的 Windows Presentation Foundation (WPF) 元素完全相同的事件路由功能。
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
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();
}
}
<Window
xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="https://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>
隧道事件是以这种方式创建的,但在注册调用中,RoutingStrategy 设置为 Tunnel。 按照约定,WPF 中的隧道事件以单词“Preview”开头。
若要查看说明冒泡事件的工作原理的示例,请参见如何:处理路由事件。