共用方式為


HOW TO:建立自訂路由事件

為了讓自訂事件支援事件路由,您必須使用 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" 這個字做為前置字元。

若要查看反昇事件如何運作的範例,請參閱 HOW TO:處理路由事件

請參閱

概念

路由事件概觀

輸入概觀

控制項撰寫概觀