中国现在的主席是谁,全国火车时刻查询,金庸群侠传4无敌版
在上一篇博文中写到了内置路由事件,其实除了内置的路由事件,我们也可以进行自定义路由事件。接下来我们一起来看一下WPF中的自定义路由事件怎么进行创建吧。
现在我们一起创建一个能够报告当前时间和当前位置信息的路由事件,一起去控件里面游览一番。现在开始创建自定义路由事件
创建继承RoutedEventArgs类的派生类ReportCurrentLocationEventArgs用来携带时间和位置消息,ClickTime属性是用来存储时间,CurrentLocation属性是用来存放位置
我们用EventManager.RegisterRoutedEvent方法来注册的参数有4个。代码如下:
public static readonly RoutedEvent ReportCurrentLocationEvent =EventManager.RegisterRoutedEvent
("ReportCurrentLocation", RoutingStrategy.Bubble, typeof(EventHandler<ReportCurrentLocationEventArgs
>), typeof(ButtonReportCurrentLocation));
第一种是Bubble是冒泡模式,这种模式是从触发点向上传递,知道最外层。
第三种是Tunnel是预览模式(隧道模式),这和冒泡的相反,向下传递。
CLR事件的封装器,不同于依赖属性的GetValue和SetValue,这里是利用Add和Remove两个函数来给路由事件分配事件处理器。
public event RoutedEventHandler ReportCurrentLocation
add { this.AddHandler(ReportCurrentLocationEvent, value); }
remove { this.RemoveHandler(ReportCurrentLocationEvent, value); }
重写OnClick方法触发设定路由事件,这是使用RaiseEvent()方法来触发
protected override void OnClick()
ReportCurrentLocationEventArgs args = new ReportCurrentLocationEventArgs(ReportCurrentLocationEvent, this);
args.ClickTime = DateTime.Now;
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
Net Core Web Api项目与在NginX下发布的方法
asp.net core3.1 引用的元包dll版本兼容性问题解决方案
IdentityServer4实现.Net Core API接口权限认证(快速入门)
ASP.NET Core MVC通过IViewLocationExpander扩展视图搜索路径的实现
网友评论