自定义路由事件

来源:互联网 发布:大数据人物画像ppt 编辑:程序博客网 时间:2024/06/03 18:54

创建自定义路由事件大体分为三个步骤:
(1)、声明并注册路由事件。
(2)、为路由事件添加CLR事件包装。
(3)、创建可以激发路由事件的方法。

下面用一个例子来展示这个过程:
首先创建一个RoutedEventArgs类的派生类,并为其添加ClickTime属性:

public class ReportTimeEventArgs : RoutedEventArgs{    public ReportTimeEventArgs(RoutedEvent routedEvent, object source)        : base(routedEvent, source) { }    public DateTime ClickTime { get; set; }}

然后创建一个Button类的派生类并按前述步骤为其添加路由事件:

public class TimeButton : Button{    //声明和注册路由事件    public static readonly RoutedEvent ReportTimeEvent = EventManager.RegisterRoutedEvent        ("ReportTime", RoutingStrategy.Bubble, typeof(EventHandler<ReportTimeEventArgs>), typeof(TimeButton));    //CLR时间包装器    public event RoutedEventHandler ReportTime    {        add { this.AddHandler(ReportTimeEvent, value); }        remove { this.RemoveHandler(ReportTimeEvent, value); }    }    //激发路由事件,借用Click事件的激发方法    protected override void OnClick()    {        base.OnClick();     //保证Button原有功能正常使用、Click事件能被激发        ReportTimeEventArgs args = new ReportTimeEventArgs(ReportTimeEvent, this);        args.ClickTime = DateTime.Now;        this.RaiseEvent(args);    }}

界面的XAML代码:

<Window x:Class="WpfWindows8.Window8_3_2"        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"        xmlns:local="clr-namespace:WpfWindows8"        xmlns:localModel="clr-namespace:WpfWindows8.Model"        mc:Ignorable="d"        Title="Routed Event" x:Name="window8_3_2" Height="300" Width="300"        localModel:TimeButton.ReportTime="ReportTimeHandler">    <Grid x:Name="grid_1" localModel:TimeButton.ReportTime="ReportTimeHandler">        <Grid x:Name="grid_2" localModel:TimeButton.ReportTime="ReportTimeHandler">            <Grid x:Name="grid_3" localModel:TimeButton.ReportTime="ReportTimeHandler">                <StackPanel x:Name="stackPanel_1"                            localModel:TimeButton.ReportTime="ReportTimeHandler">                    <ListBox x:Name="listBox"/>                    <localModel:TimeButton x:Name="timeButton" Width="80" Height="80"                                           Content="报时" localModel:TimeButton.ReportTime="ReportTimeHandler"/>                </StackPanel>            </Grid>        </Grid>    </Grid></Window>

ReportTimeHandler的代码如下:

     private void ReportTimeHandler(object sender, ReportTimeEventArgs e)        {            FrameworkElement element = sender as FrameworkElement;            string timeStr = e.ClickTime.ToLongTimeString();            string content = string.Format("{0} 到达 {1}", timeStr, element.Name);            this.listBox.Items.Add(content);        }

运行结果如下
这里写图片描述这里写图片描述

声明并注册路由事件

public static readonly RoutedEvent ReportTimeEvent = EventManager.RegisterRoutedEvent            ("ReportTime", RoutingStrategy.Bubble, typeof(EventHandler<ReportTimeEventArgs>), typeof(TimeButton));

EventManager.RegisterRoutedEvent方法有四个参数,含义分别为:
第一个参数string类型,被称为路由事件的名称,一般为RoutedEvent变量的前缀和CLR事件包装器的名称一致。
第二个参数为路由事件的策略。WPF路由事件有3种策略:
1.Bubble,冒泡式:路由事件由事件激发者出发向它上级容器一层一层路由,直至最外层容器。
2.Tunnel,隧道式:事件的路由方向正好与Bubble策略相反,由UI树的树根向事件激发控件移动。
3.Direct,直达式:模仿CLR直接事件,直接将事件消息送达事件处理器。
第三个参数用于指定事件处理器类型。
第四个参数用于指名路由事件的宿主(拥有者)是哪个类型。这个类型与第一个参数共同参与一些底层算法产生这个路由事件的Hash Code并注册到程序的路由事件列表中。

0 0