Sdílet prostřednictvím


Postupy: Zpracování více událostí pomocí vlastností události

Chcete-li použít vlastnosti události, definujete vlastnosti události ve třídě, která vyvolává události, a pak nastavíte delegáty pro vlastnosti události ve třídách, které zpracovávají události. Pokud chcete do třídy implementovat více vlastností událostí, měla by třída interně ukládat a udržovat delegáta definovaného pro každou událost. Pro každý typ události podobné poli se vygeneruje odpovídající odkaz na podpůrné pole. To může vést k zbytečným přidělením při nárůstu počtu událostí. Jako alternativu je běžným přístupem udržovat EventHandlerList , který ukládá události podle klíče.

K uložení delegátů pro každou událost můžete použít EventHandlerList třídu nebo implementovat vlastní kolekci. Třída kolekce musí poskytovat metody pro nastavení, přístup k delegátovi obslužné rutiny události a jeho načítání podle klíče události. Můžete například použít Hashtable třídu nebo odvodit vlastní třídu z DictionaryBase třídy. Podrobnosti implementace kolekce delegátů nemusí být zveřejněny mimo vaši třídu.

Každá vlastnost události v rámci třídy definuje metodu add accessor a metodu remove accessor. Metoda přidání pro vlastnost události přidá instanci delegáta vstupu do kolekce delegátů. Odebrací metoda pro vlastnost události odebere delegátní instanci z kolekce delegátů. Přístupové objekty vlastností události používají předdefinovaný klíč vlastnosti události k přidání a odebrání instancí z kolekce delegátů.

Zpracování více událostí pomocí vlastností události

  1. Definujte kolekci delegátů v rámci třídy, která vyvolává události.

  2. Definujte klíč pro každou událost.

  3. Definujte vlastnosti události ve třídě, která vyvolává události.

  4. Pomocí kolekce delegáta implementujte metody přidání a odebrání přístupového objektu pro vlastnosti události.

  5. Veřejné vlastnosti události slouží k přidání a odebrání delegátů obsluhy událostí ve třídách obsluhujících události.

Příklad

Následující příklad jazyka C# implementuje vlastnosti MouseDown události a MouseUppomocí příkazu EventHandlerList k uložení delegáta každé události. Klíčová slova konstrukcí vlastností události jsou tučně.

// 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

Viz také