Panduan: Menangani Beberapa Kejadian Menggunakan Properti Kejadian

Untuk menggunakan properti peristiwa, Anda menentukan properti peristiwa di kelas yang menaikkan peristiwa, lalu mengatur delegasi untuk properti acara di kelas yang menangani peristiwa. Untuk menerapkan beberapa properti peristiwa dalam kelas, kelas harus menyimpan dan memelihara delegasi yang ditentukan secara internal untuk setiap acara. Untuk setiap bidang-seperti-peristiwa, jenis referensi backing-field yang sesuai dihasilkan. Hal ini bisa menyebabkan alokasi yang tidak perlu ketika jumlah peristiwa meningkat. Sebagai alternatif, pendekatan umum adalah mempertahankan EventHandlerList yang menyimpan peristiwa berdasarkan kunci.

Untuk menyimpan delegasi untuk setiap acara, Anda bisa menggunakan kelas EventHandlerList, atau mengimplementasikan koleksi Anda sendiri. Kelas koleksi harus menyediakan metode untuk mengatur, mengakses, dan mengambil delegasi penanganan peristiwa berdasarkan kunci peristiwa. Misalnya, Anda bisa menggunakan kelas Hashtable, atau memperoleh kelas kustom dari kelas DictionaryBase. Detail implementasi dari koleksi delegasi tidak perlu diekspos di luar kelas Anda.

Setiap properti peristiwa dalam kelas menentukan metode tambahkan aksesor dan hapus metode aksesor. Aksesori tambahkan untuk properti acara menambahkan instans delegasi input ke koleksi delegasi. Aksesori hapus untuk properti acara menghapus instans delegasi input dari kumpulan delegasi. Aksesori properti acara menggunakan kunci yang telah ditentukan untuk properti acara untuk menambahkan dan menghapus instans dari kumpulan delegasi.

Untuk menangani beberapa peristiwa menggunakan properti peristiwa

  1. Tentukan koleksi delegasi dalam kelas yang meningkatkan acara.

  2. Tentukan kunci untuk setiap acara.

  3. Tentukan properti acara di kelas yang meningkatkan peristiwa.

  4. Gunakan koleksi delegasi untuk menerapkan metode tambahkan dan hapus aksesori untuk properti acara.

  5. Gunakan properti peristiwa publik untuk menambahkan dan menghapus delegasi penanganan aktivitas di kelas yang menangani peristiwa.

Contoh

Contoh C# berikut mengimplementasikan properti MouseDown peristiwa dan MouseUp, menggunakan EventHandlerList untuk menyimpan setiap delegasi peristiwa. Kata kunci dari konstruksi properti acara berada dalam bentuk tebal.

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

Lihat juga