如何:使用事件属性处理多个事件How to: Handle Multiple Events Using Event Properties

若要使用事件属性,请在引发事件的类中定义事件属性,然后在处理事件的类中设置事件属性的委托。To use event properties, you define the event properties in the class that raises the events, and then set the delegates for the event properties in classes that handle the events. 若要在类中实现多个事件属性,此类必须在内部存储和维护为每个事件定义的委托。To implement multiple event properties in a class, the class must internally store and maintain the delegate defined for each event. 典型的方法是实现通过事件键索引的委托集合。A typical approach is to implement a delegate collection that is indexed by an event key.

若要存储每个事件的委托,则可以使用 EventHandlerList 类,或实现自己的集合。To store the delegates for each event, you can use the EventHandlerList class, or implement your own collection. 集合类必须基于事件键提供用于设置、访问和检索事件处理程序委托的方法。The collection class must provide methods for setting, accessing, and retrieving the event handler delegate based on the event key. 例如,可以使用 Hashtable 类,或从 DictionaryBase 类中派生一个自定义的类。For example, you could use a Hashtable class, or derive a custom class from the DictionaryBase class. 委托集合的实现细节不需要在你的类的外部公开。The implementation details of the delegate collection do not need to be exposed outside your class.

类中的每个事件属性都定义了一个 add 访问器方法和一个 remove 访问器方法。Each event property within the class defines an add accessor method and a remove accessor method. 事件属性的 add 访问器将输入委托实例添加到委托集合。The add accessor for an event property adds the input delegate instance to the delegate collection. 事件属性的 remove 访问器将输入委托实例从委托集合中移除。The remove accessor for an event property removes the input delegate instance from the delegate collection. 事件属性访问器使用的预定义的事件属性键来将实例添加到委托集合或从中将实例移除。The event property accessors use the predefined key for the event property to add and remove instances from the delegate collection.

使用事件属性处理多个事件To handle multiple events using event properties

  1. 定义引发事件的类中的委托集合。Define a delegate collection within the class that raises the events.

  2. 定义每个事件的键。Define a key for each event.

  3. 定义引发事件的类中的事件属性。Define the event properties in the class that raises the events.

  4. 使用委托集合实现添加和移除事件属性的 add 和 remove 访问器方法。Use the delegate collection to implement the add and remove accessor methods for the event properties.

  5. 使用公共事件属性来添加和移除处理事件的类中的事件处理程序委托。Use the public event properties to add and remove event handler delegates in the classes that handle the events.

示例Example

下面的 C# 示例实现事件属性 MouseDownMouseUp,其中使用 EventHandlerList 来存储每个事件的委托。The following C# example implements the event properties MouseDown and MouseUp, using an EventHandlerList to store each event's delegate. 事件属性构造的关键字是粗体类型。The keywords of the event property constructs are in bold type.

// The class SampleControl defines two event properties, MouseUp and MouseDown.
ref class SampleControl : Component
{
    // :
    // Define other control methods and properties.
    // :

    // Define the delegate collection.
protected:
    EventHandlerList^ listEventDelegates;

private:
    // Define a unique key for each event.
    static Object^ mouseDownEventKey = gcnew Object();
    static Object^ mouseUpEventKey = gcnew Object();

    // Define the MouseDown event property.
public:
    SampleControl()
    {
        listEventDelegates = gcnew EventHandlerList();
    }

    event MouseEventHandler^ MouseDown
    {
        // Add the input delegate to the collection.
        void add(MouseEventHandler^ value)
        {
            listEventDelegates->AddHandler(mouseDownEventKey, value);
        }
        // Remove the input delegate from the collection.
        void remove(MouseEventHandler^ value)
        {
            listEventDelegates->RemoveHandler(mouseDownEventKey, value);
        }
        // Raise the event with the delegate specified by mouseDownEventKey
        void raise(Object^ sender, MouseEventArgs^ e)
        {
            MouseEventHandler^ mouseEventDelegate =
                (MouseEventHandler^)listEventDelegates[mouseDownEventKey];
            mouseEventDelegate(sender, e);
        }
    }

    // Define the MouseUp event property.
    event MouseEventHandler^ MouseUp
    {
        // Add the input delegate to the collection.
        void add(MouseEventHandler^ value)
        {
            listEventDelegates->AddHandler(mouseUpEventKey, value);
        }
        // Remove the input delegate from the collection.
        void remove(MouseEventHandler^ value)
        {
            listEventDelegates->RemoveHandler(mouseUpEventKey, value);
        }
        // Raise the event with the delegate specified by mouseUpEventKey
        void raise(Object^ sender, MouseEventArgs^ e)
        {
            MouseEventHandler^ mouseEventDelegate =
                (MouseEventHandler^)listEventDelegates[mouseUpEventKey];
            mouseEventDelegate(sender, e);
        }
    }
};
// 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

另请参阅See also