HOW TO:發行符合 .NET Framework 方針的事件 (C# 程式設計手冊)

下列程序會示範如何將採用標準 .NET Framework 模式的事件加入至您的類別和結構 (Struct) 中。 .NET Framework 類別庫 (Class Library) 內的所有事件都是根據 EventHandler 委派 (Delegate) 而建立,此委派定義如下:

public delegate void EventHandler(object sender, EventArgs e);
注意事項注意事項

.NET Framework 2.0 引進此委派的泛型版本,即 EventHandler<TEventArgs>。下列範例說明如何使用兩種版本。

雖然您所定義之類別內的事件可以以任何有效的委派型別 (包括傳回值的委派) 為基礎,不過一般會建議您使用 EventHandler,讓您的事件以 .NET Framework 模式為基礎,如下列範例所示:

若要根據 EventHandler 模式來發行事件

  1. (如果您不需要以事件傳送自訂的資料,請跳過這個步驟,並執行步驟 3a)。 以發行者類別和訂閱者類別可見的範圍宣告您自訂資料的類別。 然後加入必要的成員來保存您的自訂事件資料。 在此範例中,會傳回一個簡單的字串。

    public class CustomEventArgs : EventArgs
    {
        public CustomEventArgs(string s)
        {
            msg = s;
        }
        private string msg;
        public string Message
        {
            get { return msg; }
        } 
    }
    
  2. (如果您正在使用 EventHandler<TEventArgs> 的泛型版本,請跳過這個步驟)。 在發行類別內宣告委派, 然後為它提供名稱,並以 EventHandler 做為結尾。 第二個參數會指定自訂的 EventArgs 型別。

    public delegate void CustomEventHandler(object sender, CustomEventArgs a);
    
  3. 使用下列其中一個步驟,在發行類別內宣告事件。

    1. 如果沒有自訂的 EventArgs 類別,則您的 Event 型別將會是非泛型的 EventHandler 委派。 您不需要宣告委派,因為它已在您建立 C# 專案時所包含的 System 命名空間內宣告。 加入下列程式碼至發行者類別。

      public event EventHandler RaiseCustomEvent;
      
    2. 如果您是使用 EventHandler 的非泛型版本,而且您擁有衍生自 EventArgs 的自訂類別,請在發行類別內宣告事件,並使用步驟 2 中的委派當做型別。

      public event CustomEventHandler RaiseCustomEvent;
      
    3. 如果您正在使用泛型版本,則不需要使用自訂委派, 而是在發行類別中,將事件型別指定為 EventHandler<CustomEventArgs>,以取代角括弧內的類別名稱。

      public event EventHandler<CustomEventArgs> RaiseCustomEvent;
      

範例

下列範例將使用自訂 EventArgs 類別和 EventHandler<TEventArgs> 做為事件類型示範前述步驟。

namespace DotNetEvents
{
    using System;
    using System.Collections.Generic;

    // Define a class to hold custom event info
    public class CustomEventArgs : EventArgs
    {
        public CustomEventArgs(string s)
        {
            message = s;
        }
        private string message;

        public string Message
        {
            get { return message; }
            set { message = value; }
        }
    }

    // Class that publishes an event
    class Publisher
    {

        // Declare the event using EventHandler<T>
        public event EventHandler<CustomEventArgs> RaiseCustomEvent;

        public void DoSomething()
        {
            // Write some code that does something useful here
            // then raise the event. You can also raise an event
            // before you execute a block of code.
            OnRaiseCustomEvent(new CustomEventArgs("Did something"));

        }

        // Wrap event invocations inside a protected virtual method
        // to allow derived classes to override the event invocation behavior
        protected virtual void OnRaiseCustomEvent(CustomEventArgs e)
        {
            // Make a temporary copy of the event to avoid possibility of
            // a race condition if the last subscriber unsubscribes
            // immediately after the null check and before the event is raised.
            EventHandler<CustomEventArgs> handler = RaiseCustomEvent;

            // Event will be null if there are no subscribers
            if (handler != null)
            {
                // Format the string to send inside the CustomEventArgs parameter
                e.Message += String.Format(" at {0}", DateTime.Now.ToString());

                // Use the () operator to raise the event.
                handler(this, e);
            }
        }
    }

    //Class that subscribes to an event
    class Subscriber
    {
        private string id;
        public Subscriber(string ID, Publisher pub)
        {
            id = ID;
            // Subscribe to the event using C# 2.0 syntax
            pub.RaiseCustomEvent += HandleCustomEvent;
        }

        // Define what actions to take when the event is raised.
        void HandleCustomEvent(object sender, CustomEventArgs e)
        {
            Console.WriteLine(id + " received this message: {0}", e.Message);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Publisher pub = new Publisher();
            Subscriber sub1 = new Subscriber("sub1", pub);
            Subscriber sub2 = new Subscriber("sub2", pub);

            // Call the method that raises the event.
            pub.DoSomething();

            // Keep the console window open
            Console.WriteLine("Press Enter to close this window.");
            Console.ReadLine();

        }
    }
}

請參閱

參考

事件 (C# 程式設計手冊)

委派 (C# 程式設計手冊)

Delegate

概念

C# 程式設計手冊