【winfrom】事件与委托

  //事件 
        //与委托   委托的一个实例,前面加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;
        }
    }

  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值