Udostępnij za pośrednictwem


Jak: publikowanie zdarzenia, które odpowiadają.NET Framework wytycznych (Podręcznik programowania C#)

W poniższej procedurze przedstawiono sposób dodawania wydarzenia, które są zgodne ze standardem .NET Framework wzorzec do klas i strukturach.Wszystkie zdarzenia w .NET Framework Biblioteka klas są oparte na EventHandler pełnomocnika, który jest zdefiniowany w następujący sposób:

public delegate void EventHandler(object sender, EventArgs e);

[!UWAGA]

.NET Framework 2.0 Wprowadzono rodzajowy wersję tego obiektu delegowanego EventHandler<TEventArgs>.Jak używać obu wersji można znaleźć w poniższych przykładach.

Chociaż zdarzenia w klasach zdefiniowanych przez użytkownika mogą być oparte na dowolnego typu delegata prawidłowe nawet delegatów, które zwracają wartość, zwykle zaleca oprzeć wydarzeń na .NET Framework wzorca za pomocą EventHandler, jak pokazano w poniższym przykładzie.

Aby opublikować zdarzenia na podstawie wzoru EventHandler

  1. (Pomiń ten krok i przejdź do kroku 3a, jeśli nie trzeba wysyłać dane niestandardowe z wydarzenia). Zadeklarować klasy niestandardowej danych na zakres, który jest widoczny dla klas zarówno wydawcy, jak i subskrybenta.Następnie dodaj członków wymagane do przechowywania danych zdarzenia niestandardowego.W tym przykładzie zostanie zwrócony ciąg proste.

    public class CustomEventArgs : EventArgs
    {
        public CustomEventArgs(string s)
        {
            msg = s;
        }
        private string msg;
        public string Message
        {
            get { return msg; }
        } 
    }
    
  2. (Pomiń ten krok, jeśli używasz wersji generic EventHandler<TEventArgs> .) Zadeklarować delegata w klasie publikowania.Nadaj nazwę, która kończy się na EventHandler.Drugi parametr określa niestandardowego typu EventArgs.

    public delegate void CustomEventHandler(object sender, CustomEventArgs a);
    
  3. Zdarzenia w klasie publikowania należy zadeklarować przy użyciu jednej z następujących czynności.

    1. Jeśli nie niestandardowe klasy EventArgs typu zdarzenie będzie pełnomocnik EventHandler-uniwersalne.Nie trzeba zadeklarować obiektu delegowanego, ponieważ jest już zadeklarowany w System obszaru nazw, który jest dołączony podczas tworzenia projektu języka C#.Dodaj następujący kod do klasy programu publisher.

      public event EventHandler RaiseCustomEvent;
      
    2. Jeśli używasz wersji nierodzajową EventHandler i niestandardowej klasy pochodne EventArgs, zadeklarować wydarzenia wewnątrz klasy publikowania i używanie pełnomocnika od kroku 2 jako typu.

      public event CustomEventHandler RaiseCustomEvent;
      
    3. Jeśli używasz wersji rodzajowy nie ma potrzeby niestandardowego obiektu delegowanego.Zamiast tego, w klasie publikacji, należy określić typ sieci zdarzenia jako EventHandler<CustomEventArgs>, podstawiając nazwę klasy między nawiasy ostre.

      public event EventHandler<CustomEventArgs> RaiseCustomEvent;
      

Przykład

W poniższym przykładzie zademonstrowano poprzednie kroki przy użyciu niestandardowej klasy EventArgs i EventHandler<TEventArgs> jako typ zdarzenia.

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();

        }
    }
}

Zobacz też

Informacje

Zdarzenia (Podręcznik programowania C#)

Pełnomocnicy (Podręcznik programowania C#)

Delegate

Koncepcje

Podręcznik programowania C#