.Net Framework中的标准委托和事件_1

.Net Framework中的标准委托,已经定义在命名空间System中,

namespace System
{
    public delegate void EventHandler(object sender, EventArgs e);
}

.Net Framwork类库中的所有事件均基于EventHandler委托。

其中EventArgs参数是可以自定义,必须继承EventArgs类:

public class CustomEventArgs:EventArgs

发布事件有三种方式:

1. 使用.net framework标准委托 

public event EventHandler RaiseCustomEvent;

2. 自定义EventArgs参数

public event CustomEventHandler RaiseCustomEvent;

3. 自定义EventArgs参数,泛型的表达方式

public event EventHandler<CustomEventArgs> RaiseCustomEvent;

如何:发布符合.net framework准则的事件

首先分两个类:

1. 发行者类(Publisher),发送Sends(引发Raise)事件的类,这个类做两件事

  • 发布事件
  • 编写事件的触发程序

     窗体(Form)和控件(control)的类中都发布了事件(Load,Click等),事件的触发程序同样能找到,如下:

protected virtual void OnClick(EventArgs e);

2. 订阅者类(Subscriber),接收Receive(处理Handle)事件的类,这个类做两件事

  • 注册事件
  • 编写事件的处理程序

     当你双击Form1中的一个按钮Button1,会直接进入

        private void button1_Click(object sender, EventArgs e)
        {
        }

    其实就是在写事件的处理程序;在Form1.Designer.cs中会自动注册按钮双击事件

this.button1.Click += new System.EventHandler(this.button1_Click);

下面来看一个例子,是一个比较标准的方式添加自定义的事件:

namespace DotNetEvents
{
    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();

            Console.WriteLine("Press Enter to close this window.");
            Console.ReadKey();
        }
    }

    //Define a class to hold custom event info
    public class CustomEventArgs:EventArgs
    {
        private string message;
        public CustomEventArgs (string s)
        {
            message = s;
        }
        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 oveeride 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 raise
            if(RaiseCustomEvent !=null )
            {
                e.Message = String.Format(" at {0}", DateTime.Now.ToString());
                RaiseCustomEvent(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 suing C# 2.0 syntax
            pub.RaiseCustomEvent += HandleCustomEvent;
        }

        //Define what action to take when the event is raised
        void HandleCustomEvent(object sender,CustomEventArgs e)
        {
            Console.WriteLine(id + " receive this message: {0}", e.Message);
        }
    }
}
原文地址:https://www.cnblogs.com/xiao9426926/p/5976249.html