#600 – 注册一个路由事件(Registering a Routed Event)
来源:互联网 发布:卜柯文淘宝星店 编辑:程序博客网 时间:2024/06/06 16:57
当你在类中要实现一个路由事件的时候,你最终都是要创建一个RoutedEvent 类型的实例。
你创建RoutedEvent 类型的实例,其实就是间接的调用了EventManager.RegisterRoutedEvent方法。这个方法接收你想要传递的一些事件信息,并返回一个静态的RoutedEvent字段。事件通常会在静态的构造函数中或者事件声明的时候注册,所以对于你来说,到最后你只得到一个RoutedEvent,不管多少个同类型的RoutedEvent 实例被创建。
// Define/create the routed event objectpublic static readonly RoutedEvent ClickEvent = EventManager.RegisterRoutedEvent("Click"
, RoutingStrategy.Bubble,
typeof
(RoutedEventHandler),
typeof
(ButtonBase));
RegisterRoutedEvent 方法有下面几个参数:
- name ——事件的名称
- 路由策略——隧道、冒泡或者直接
- 事件处理函数的类型——事件处理函数的委托
- 事件拥有者的类型——拥有这个事件的类的类型
为了帮助理解路由事件在WPF中的工作方式,下面是ButtonBase 中Click 事件的注册的例子。
public abstract class ButtonBase : ContentControl, ICommandSource{ // Define/create the routed event object public static readonly RoutedEvent ClickEvent = EventManager.RegisterRoutedEvent("Click", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(ButtonBase)); // CLR event wrapper, adds/removes handlers public event RoutedEventHandler Click { add { AddHandler(ClickEvent, value); } remove { RemoveHandler(ClickEvent, value); } } // Method used internally to fire the Click event protected virtual void OnClick() { RoutedEventArgs newEvent = new RoutedEventArgs(ButtonBase.ClickEvent, this); RaiseEvent(newEvent); }}AddHandler 和RaiseEvent 方法定义在了UIElement类中。
原文地址:https://wpf.2000things.com/2012/07/11/600-registering-a-routed-event/
1 0
- #600 – 注册一个路由事件(Registering a Routed Event)
- #604 – 定义一个路由事件(Defining a New Routed Event)
- #606 – 重用(另一个类中)已经存在的路由事件(Reusing an Existing Routed Event in Your Class)
- #117 – Registering a Dependency Property(注册一个依赖属性)
- Routed Event & Routed Commands
- Routed event and command
- 将应用程序注册为URL协议(Registering an Application to a URL Protocol)
- COM专题三---生成和注册代理根服务器(Building and Registering a Proxy DLL)
- (WPF学习记录)第九章 Routed输入事件
- WPF 路由事件 Event Routing
- (七)注册/注销event事件
- C#事件(event)的一个实例
- WPF与MVVM的实现(五)注册路由事件
- Event事件的注册和注销
- 事件(event)
- 事件(Event)
- event事件(1)
- 创建事件(event)
- [号外]Android Rom知名定制公司Cyanogen关闭服务!
- MFC将某一函数的返回值显示出来
- 分词技术
- 二维随机变量的函数的分布
- base64
- #600 – 注册一个路由事件(Registering a Routed Event)
- Windows Server2012R2 FTP服务器配置
- Python安装lxml出错:error: Microsoft Visual C++ 9.0 is required.
- HTML 表单的练习记录篇
- 求解立方根
- centos 6.x/7.x上安装git
- python——赋值与深浅拷贝
- 浅谈三层架构
- Ajax小实例2验证注册框-经验总结