类型事件定义
2010-03-07 22:19
176 查看
要使用事件属性(Visual Basic 2005 中的自定义事件),请在引发事件的类中定义事件属性,然后在处理事件的类中设置事件属性的委托。要在一个类中实现多个事件属性,该类必须在内部存储和维护为每个事件定义的委托。一种典型方法是实现通过事件键进行索引的委托集合。
若要存储每个事件的委托,可以使用 EventHandlerList 类或实现您自己的集合。集合类必须提供用于基于事件键设置、访问和检索事件处理程序委托的方法。例如,可以使用 Hashtable 类或从 DictionaryBase 类派生一个自定义类。不需要在类以外公开委托集合的实现详细信息。
类中的每个事件属性定义一个 add 访问器方法和一个 remove 访问器方法。事件属性的 add 访问器将输入委托实例添加到委托集合。事件属性的 remove 访问器从委托集合中移除输入委托实例。事件属性访问器使用事件属性的预定义键在委托集合中添加和从委托集合中移除实例。
定义每个事件的键。
在引发事件的类中定义事件属性。
使用委托集合实现事件属性的 add 访问器方法和 remove 访问器方法。
使用公共事件属性可在处理事件的类中添加和移除事件处理程序委托。
下面的 C# 示例使用 EventHandlerList 存储每个事件的委托,从此来实现事件属性 MouseDown 和 MouseUp。事件属性构造的关键字用粗体表示。
若要存储每个事件的委托,可以使用 EventHandlerList 类或实现您自己的集合。集合类必须提供用于基于事件键设置、访问和检索事件处理程序委托的方法。例如,可以使用 Hashtable 类或从 DictionaryBase 类派生一个自定义类。不需要在类以外公开委托集合的实现详细信息。
类中的每个事件属性定义一个 add 访问器方法和一个 remove 访问器方法。事件属性的 add 访问器将输入委托实例添加到委托集合。事件属性的 remove 访问器从委托集合中移除输入委托实例。事件属性访问器使用事件属性的预定义键在委托集合中添加和从委托集合中移除实例。
使用事件属性处理多个事件
在引发事件的类中定义一个委托集合。定义每个事件的键。
在引发事件的类中定义事件属性。
使用委托集合实现事件属性的 add 访问器方法和 remove 访问器方法。
使用公共事件属性可在处理事件的类中添加和移除事件处理程序委托。
示例
下面的 C# 示例使用 EventHandlerList 存储每个事件的委托,从此来实现事件属性 MouseDown 和 MouseUp。事件属性构造的关键字用粗体表示。Visual Basic 2005 中不支持事件属性。 |
C# | 复制代码 |
---|---|
// The class SampleControl defines two event properties, MouseUp and MouseDown. class SampleControl: Component { // : // Define other control methods and properties. // : // Define the delegate collection. protected EventHandlerList listEventDelegates = new EventHandlerList(); // Define a unique key for each event. static readonly object mouseDownEventKey = new object(); static readonly object mouseUpEventKey = new object(); // Define the MouseDown event property. http://ddue.schemas.microsoft.com/authoring/2003/5 NS = "http://ddue.schemas.microsoft.com/authoring/2003/5" />public event MouseEventHandler MouseDown { // Add the input delegate to the collection. add { listEventDelegates.AddHandler(mouseDownEventKey, value); } // Remove the input delegate from the collection. remove { listEventDelegates.RemoveHandler(mouseDownEventKey, value); } } // Define the MouseUp event property. public event MouseEventHandler MouseUp { // Add the input delegate to the collection. add { listEventDelegates.AddHandler(mouseUpEventKey, value); } // Remove the input delegate from the collection. remove { listEventDelegates.RemoveHandler(mouseUpEventKey, value); } } } |
相关文章推荐
- 定义事件过程类型
- C# 001 --- 正确的删除一个控件的事件函数 --- 解决错误: “A”不包含“B”的定义,并且找不到可接受类型为“A”的第一个参数的扩展方法“B”(是否缺少 using 指令或程序集引用?)
- 类型事件定义
- Duilib 事件类型定义
- 关于页面不包含"事件_Click"的定义 并且找不到可接受页面类型的第一个参数的扩展方法"事件_Click" 的解决方案
- .net 在类中的Event事件,为什么可以定义为static?而委托类型却不可以
- C++ 解析什么叫“class”类型重定义还有解决方法
- 了解C#中的预定义事件处理机制【整理】
- wxWidgets事件定义
- 用于创建实现 System.ComponentModel.INotifyPropertyChanged 接口的动态类型,并添加各个 public 属性的定义
- Atitit.变量的定义 获取 储存 物理结构 基本类型简化 隐式转换 类型推导 与底层原理 attilaxDSL
- java实现定义栈的数据结构,请在该类型中实现一个能够得到栈最小元素的min函数
- Objective-C 基础,类和对象,方法和消息,已声明的属性和存取方法,块对象,协议和范畴类,预定义类型和编码策略
- C#简单问题,不简单的原理:不能局部定义自定义类型(不含匿名类型)
- C++定义容器元素为容器类型时的>操作符
- VC定义的三种类型DLL有什么区别
- 指针相关的预定义类型
- Java事件类型小结
- 关于loose.dtd和xhtml1-transitional.dtd等文档类型定义模型中CSS失效的解决办法。
- 深入理解DOM事件类型系列第五篇——剪贴板事件