类型事件定义
来源:互联网 发布:fotor mac版 编辑:程序博客网 时间:2024/06/05 00:11
要使用事件属性(Visual Basic 2005 中的自定义事件),请在引发事件的类中定义事件属性,然后在处理事件的类中设置事件属性的委托。要在一个类中实现多个事件属性,该类必须在内部存储和维护为每个事件定义的委托。一种典型方法是实现通过事件键进行索引的委托集合。
若要存储每个事件的委托,可以使用
类中的每个事件属性定义一个 add 访问器方法和一个 remove 访问器方法。事件属性的 add 访问器将输入委托实例添加到委托集合。事件属性的 remove 访问器从委托集合中移除输入委托实例。事件属性访问器使用事件属性的预定义键在委托集合中添加和从委托集合中移除实例。
使用事件属性处理多个事件
-
在引发事件的类中定义一个委托集合。
-
定义每个事件的键。
-
在引发事件的类中定义事件属性。
-
使用委托集合实现事件属性的 add 访问器方法和 remove 访问器方法。
-
使用公共事件属性可在处理事件的类中添加和移除事件处理程序委托。
示例
下面的 C# 示例使用
Visual Basic 2005 中不支持事件属性。
// 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.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); } }}
- 类型事件定义
- 定义事件过程类型
- Duilib 事件类型定义
- 类型定义
- C#事件-定义事件
- ------事件与委托 (定义与用法) 事件与委托的区别 (委托是类型,事件是对象)
- 事件类型
- 事件类型
- 事件类型
- 事件类型
- 事件类型
- 事件类型--UI事件
- 事件类型--焦点事件
- 事件---事件类型
- c#事件的定义
- 再谈CLR:事件定义
- C#定义静态事件
- js定义回车事件
- ADO.NET Data Service:如何做身份验证
- 工厂模式(Factory)
- 单件模式(Singleton)和锁定(lock)
- 修改cmd的默认路径
- T4 Templates: A Quick-Start Guide for ASP.NET MVC Developers
- 类型事件定义
- What's New in Visual Studio 2010
- Dynamic 和 Dynamic Language Runtime(DLR)
- What's New in the Visual Studio Debugger
- Silverlight3 加载其他xap
- Silverlight应用程序 的隔离存储
- IE 8 中选项卡通过颜色分组
- 在Silverlight中读取ADO.NET数据服务的两种方式
- Silverlight隔离存储(续)