若要使用事件属性,请在引发事件的类中定义事件属性,然后在处理事件的类中为事件属性设置委托。 若要在类中实现多个事件属性,该类应在内部存储和维护为每个事件定义的委托。 对于每个字段式事件,将生成相应的后备字段引用类型。 当事件数增加时,这可能会导致不必要的分配。 作为替代方案,一个常见的方法是维护一个通过键存储事件的EventHandlerList。
若要存储每个事件的委托,可以使用EventHandlerList类,或实现自己的集合。 集合类必须提供基于事件键设置、访问和检索事件处理程序委托的方法。 例如,可以使用 Hashtable 类,或从 DictionaryBase 类派生自定义类。 委托集合的实现详细信息不需要在类外部公开。
类中的每个事件属性都定义了一个 add 访问器方法和一个 remove 访问器方法。 事件属性的 add 访问器将输入委托实例添加到委托集合。 事件属性的 remove 访问器从委托集合中删除输入委托实例。 事件属性访问器使用事件属性的预定义键从委托集合中添加和删除实例。
使用事件属性处理多个事件
在引发事件的类中定义委托集合。
为每个事件定义一个密钥。
在引发事件的类中定义事件属性。
使用委托集合来实现事件属性的添加与移除访问器方法。
使用公共事件属性在处理事件的类中添加和移除事件处理程序委托。
示例:
以下 C# 示例实现了事件属性 MouseDown
和 MouseUp
,并使用 EventHandlerList 来存储每个事件的委托。 事件属性构造的关键字采用粗体类型。
// 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);
}
}
// Raise the event with the delegate specified by mouseDownEventKey
private void OnMouseDown(MouseEventArgs e)
{
MouseEventHandler mouseEventDelegate =
(MouseEventHandler)listEventDelegates[mouseDownEventKey];
mouseEventDelegate(this, e);
}
// 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);
}
}
// Raise the event with the delegate specified by mouseUpEventKey
private void OnMouseUp(MouseEventArgs e)
{
MouseEventHandler mouseEventDelegate =
(MouseEventHandler)listEventDelegates[mouseUpEventKey];
mouseEventDelegate(this, e);
}
}
' The class SampleControl defines two event properties, MouseUp and MouseDown.
Class SampleControl
Inherits Component
' :
' Define other control methods and properties.
' :
' Define the delegate collection.
Protected listEventDelegates As New EventHandlerList()
' Define a unique key for each event.
Shared ReadOnly mouseDownEventKey As New Object()
Shared ReadOnly mouseUpEventKey As New Object()
' Define the MouseDown event property.
Public Custom Event MouseDown As MouseEventHandler
' Add the input delegate to the collection.
AddHandler(Value As MouseEventHandler)
listEventDelegates.AddHandler(mouseDownEventKey, Value)
End AddHandler
' Remove the input delegate from the collection.
RemoveHandler(Value As MouseEventHandler)
listEventDelegates.RemoveHandler(mouseDownEventKey, Value)
End RemoveHandler
' Raise the event with the delegate specified by mouseDownEventKey
RaiseEvent(sender As Object, e As MouseEventArgs)
Dim mouseEventDelegate As MouseEventHandler = _
listEventDelegates(mouseDownEventKey)
mouseEventDelegate(sender, e)
End RaiseEvent
End Event
' Define the MouseUp event property.
Public Custom Event MouseUp As MouseEventHandler
' Add the input delegate to the collection.
AddHandler(Value As MouseEventHandler)
listEventDelegates.AddHandler(mouseUpEventKey, Value)
End AddHandler
' Remove the input delegate from the collection.
RemoveHandler(Value As MouseEventHandler)
listEventDelegates.RemoveHandler(mouseUpEventKey, Value)
End RemoveHandler
' Raise the event with the delegate specified by mouseDownUpKey
RaiseEvent(sender As Object, e As MouseEventArgs)
Dim mouseEventDelegate As MouseEventHandler = _
listEventDelegates(mouseUpEventKey)
mouseEventDelegate(sender, e)
End RaiseEvent
End Event
End Class