使用UIElement.AddHandler捕获已被处理的RoutedEvent

UIElement.AddHandler 方法 (RoutedEvent, Delegate)

将指定的 路由事件的一路由事件 处理程序,将处理程序添加到当前元素的处理程序集合。

可以将同一事件多个纪元的同一处理程序,而不引发异常。但是,在中,在处理事件时,处理程序实际调用多次。因此,请考虑此行为如何可能有应考虑在您的处理程序实现的副作用。

通常使用此方法提供实现 “添加”自定义路由事件的 Microsoft .NET 事件访问模式的访问器。

MSDN源码:

// 摘要:

// 为指定的路由事件添加路由事件处理程序,并将该处理程序添加到当前元素的处理程序集合中。将 handledEventsToo 指定为 true 时,可为已标记为由其他元素在事件路由过程中处理的路由事件调用所提供的处理程序。

//

// 参数:

// routedEvent:

// 要处理的路由事件的标识符。

//

// handler:

// 对处理程序实现的引用。

//

// handledEventsToo:

// 如果为 true,则将按以下方式注册处理程序:即使路由事件在其事件数据中标记为已处理,也会调用该处理程序;如果为 false,则使用默认条件注册处理程序,即当路由事件已标记为已处理时,将不调用该处理程序。默认值为

// false。不要例行地请求重新处理路由事件。有关更多信息,请参阅“备注”。

//

// 异常:

// System.ArgumentNullException:

// routedEvent 或 handler 为 null.

//

// System.ArgumentException:

// routedEvent 不表示支持的路由事件。-或- handler 不实现支持的委托。

//

// System.NotImplementedException:

// 试图为当前平台的变体不支持的事件添加处理程序。

public void AddHandler(RoutedEvent routedEvent, Delegate handler, bool handledEventsToo);

 

1.自我简单通过Button实现代码:

button1.AddHandler(Button.MouseLeftButtonDownEvent, new MouseButtonEventHandler(myListBox_MouseLeftButtonDown), true);



public void myListBox_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)

{

MessageBox.Show("Look");

}

 

2.自定义事件支持事件路由

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="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>

 

你可能感兴趣的:(handler)