若要使用事件屬性,您可以在引發事件的類別中定義事件屬性,然後在處理事件的類別中設定事件屬性的委派。 若要在類別中實作多個事件屬性,類別應該在內部儲存和維護針對每個事件定義的委派。 針對每個類似欄位之事件,會產生相應的基礎欄位參考型別。 當事件數目增加時,這可能會導致不必要的配置。 使用 EventHandlerList 來依索引鍵儲存事件是常見的方法。
若要儲存每個事件的委派,您可以使用 EventHandlerList 類別,或實作您自己的集合。 集合類別必須提供方法,以便根據事件鍵值來設定、存取和擷取事件處理程式委派。 例如,您可以使用 Hashtable 類別,或從 DictionaryBase 類別衍生自定義類別。 委派集合的實作詳細數據不需要在類別外部公開。
類別中的每個事件屬性都會定義 add 存取子方法和 remove 存取子方法。 事件屬性的 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