如何:建立自訂路由事件

若要讓自訂事件支援事件路由,您必須使用 RegisterRoutedEvent 方法註冊 RoutedEvent 。 此範例示範建立自訂路由事件的基本概念。

範例

如下列範例所示,您會先使用 RegisterRoutedEvent 方法註冊 RoutedEvent 。 依照慣例, RoutedEvent 靜態功能變數名稱應該以後綴 事件 結尾。 在此範例中,事件的名稱是 Tap ,而事件的路由策略為 Bubble 。 註冊呼叫之後,您可以為事件提供 add-and-remove Common Language Runtime (CLR) 事件存取子。

請注意,即使透過此特定範例中的 OnTap 虛擬方法引發事件,引發事件或事件回應變更的方式還是取決於您的需求。

另請注意,此範例基本上會實作 的整個子類別;該子類別 Button 會建置為個別元件,然後在個別的 XAML 頁面上具現化為自訂類別。 這是為了說明子類別化控制項可以插入由其他控制群組成的樹狀結構的概念,在此情況下,這些控制項上的自訂事件與任何原生 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>

通道事件會以相同方式建立,但在 RoutingStrategy 註冊呼叫中設定為 Tunnel 。 依照慣例,WPF 中的通道事件前面會加上 「Preview」 一詞。

若要查看事件反昇事件運作方式的範例,請參閱處理路由事件

另請參閱