【winfrom】事件与委托

时间:2023-01-19 01:00:51
  //事件 
        //与委托   委托的一个实例,前面加event---- 事件

        //  委托 (声明   触发)  发布   (注册事件(订阅)   处理事件)订阅者
        

        private void button1_Click(object sender, EventArgs e)
        {
            Children children = new Children();

            //children.CryAction = () => { Console.WriteLine("宝宝哭了!"); };
            //children.CryAction += () => { Console.WriteLine("妈妈说别哭了!"); };
            //children.CryAction += () => { Console.WriteLine("爸爸也说别哭了!"); };
            //children.CryAction += () => { Console.WriteLine("奶奶也说哭了!"); };
            //children.CryAction += () => { Console.WriteLine("宝宝笑了!"); };

            ////children.CryActionHandler();

            //children.CryAction.Invoke();

            //children.CryActionEvent +=()=>{ Console.WriteLine("宝宝哭了!"); };//不能用=运算符
            //children.CryActionEvent += () => { Console.WriteLine("妈妈说别哭了!"); };
            //children.CryActionEvent += () => { Console.WriteLine("爸爸也说别哭了!"); };
            //children.CryActionEvent += () => { Console.WriteLine("奶奶也说哭了!"); };
            //children.CryActionEvent += () => { Console.WriteLine("宝宝笑了!"); };

            //children.CryEventHandlerEvent();


            children.MyCryEventHanlder += (o, ee) =>
            {
                ee.CryMsg = "妈妈说别哭了!";
                Console.WriteLine("{0}",ee.CryMsg);
            };
            children.MyCryEventHanlder += (o, ee) =>
            {
                ee.CryMsg = "爸爸也说别哭了!";
                Console.WriteLine("{0}", ee.CryMsg);
            };
            children.MyCryEventHanlder += (o, ee) =>
            {
                ee.CryMsg = "奶奶也说哭了!";
                Console.WriteLine("{0}", ee.CryMsg);
            };
            children.MyCryEventHanlder += (o, ee) =>
            {
                ee.CryMsg = "宝宝笑了!";
                Console.WriteLine("{0}", ee.CryMsg);
            };

            children.OnCryEvent("宝宝哭了!");

        }

  Children.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace WinEvent
{
    public class Children
    {
        public delegate void CryEventHandler();

        public CryEventHandler CryAction;

        public event CryEventHandler CryActionEvent;//事件的声明 不能在外面Invoke,包括它的子类也不行

        public event Action<object, MyEventArgs> MyCryEventHanlder;

        public void CryActionHandler()
        {
            if(CryAction!=null)
            {
                CryAction.Invoke();
            }
        }

        public void CryEventHandlerEvent()
        {
            if (CryActionEvent != null)
                CryActionEvent.Invoke();
        }


        private void MyCryEvent(MyEventArgs e)
        {
            if(MyCryEventHanlder!=null)
            {
                foreach (Action<object, MyEventArgs> f in MyCryEventHanlder.GetInvocationList())
                {
                    f.Invoke(this, e);
                   
                    Thread.Sleep(1000);
                }
                //MyCryEventHanlder.Invoke(this, e);
            }
        }

        public void OnCryEvent(string msg)
        {
            MyEventArgs e = new MyEventArgs(msg);
            if (msg != "")
                MyCryEvent(e);
        }





        


    }
}

  MyEventArgs.cs

 public class MyEventArgs:EventArgs
    {
        public string CryMsg { get; set; }

        public MyEventArgs(string msg)
        {
            this.CryMsg = msg;
        }
    }