关于“事件”的定义,请去问Google吧,本文不引用官方的术语。
废话不多说,先定义一下场景:
微波炉,大家生活中都有用过,当你关上微波炉的门时便自动加热。“关门”和“加热”便是两个动作。注意,当关上门的时候便开始加热,这里“当关上门”便是一个“事件”的触发条件。触发了什么?“加热”这个动作。
我相信你完全能理解我上面所说的内容吧?那么接下来我便一步一步来实现我的题目“C#事件Event”
第一步:定义我们的对象
对象一:微波炉
/// <summary> /// 微波炉 /// </summary> class Microwave { }
对象二:加热器(微波炉内置)
/// <summary> /// 加热器 /// </summary> class Heater { }
第二步:添加动作(或称行为)
我们提过的,“关门” 和 “加热” 两个动作
/// <summary> /// 微波炉 /// </summary> class Microwave { /// <summary> /// 关门 /// </summary> public void CloseDoor() { Console.WriteLine("Now the door is closed!"); } } /// <summary> /// 加热器 /// </summary> class Heater { /// <summary> /// 加热 /// </summary> public void Heating() { Console.WriteLine("The food is heating!"); Console.ReadLine(); } }
第三步:定义我们的“事件”(出场)
/// <summary> /// 微波炉 /// </summary> class Microwave {
public delegate void DoorCloseHandler(); public event DoorCloseHandler DoorCloseEvent; /// <summary> /// 关门 /// </summary> public void CloseDoor() { Console.WriteLine("Now the door is closed!"); } }
这里我们给“门”定义“关闭”事件,也就是下面那两句:
public delegate void DoorCloseHandler(); public event DoorCloseHandler DoorCloseEvent;
“事件”有了,什么时候执行呢?当然是“关上门”后执行了。
/// <summary> /// 微波炉 /// </summary> class Microwave { public delegate void DoorCloseHandler(); public event DoorCloseHandler DoorCloseEvent; /// <summary> /// 关门 /// </summary> public void CloseDoor() { Console.WriteLine("Now the door is closed!"); OnDoorClosed(); } public void OnDoorClosed() { if (DoorCloseEvent != null) DoorCloseEvent(); } }
看到了吗?在我们之前定义的 CloseDoor 方法后面调用我们的“事件”。
到这里为止,我们知道了事件的“发起者”(关门动作),事件的“要做什么”(加热),事件“什么时候执行”(关上门),还缺点什么呢?
串起来喽,也就是俗称的“绑定”它。
/// <summary> /// 微波炉 /// </summary> class Microwave { public delegate void DoorCloseHandler(); public event DoorCloseHandler DoorCloseEvent; /// <summary> /// 关门 /// </summary> public void CloseDoor() { Console.WriteLine("Now the door is closed!"); OnDoorClosed(); } public void OnDoorClosed() { if (DoorCloseEvent != null) DoorCloseEvent(); } public Microwave() { Heater heater = new Heater(); this.DoorCloseEvent += heater.Heating; } }
我选择了在厂家生产微波炉的时候就把他们绑起来(我是想说构造函数来着),不是这样,我不会买这微波炉的,你呢?
大功告成,让我们“关门”试试吧!
class Program { static void Main(string[] args) { Microwave wave = new Microwave(); wave.CloseDoor(); } }
最后附上完整代码:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Event { class Program { static void Main(string[] args) { Microwave wave = new Microwave(); wave.CloseDoor(); } } /// <summary> /// 微波炉 /// </summary> class Microwave { public delegate void DoorCloseHandler(); public event DoorCloseHandler DoorCloseEvent; /// <summary> /// 关门 /// </summary> public void CloseDoor() { Console.WriteLine("Now the door is closed!"); OnDoorClosed(); } public void OnDoorClosed() { if (DoorCloseEvent != null) DoorCloseEvent(); } public Microwave() { Heater heater = new Heater(); this.DoorCloseEvent += heater.Heating; } } /// <summary> /// 加热器 /// </summary> class Heater { /// <summary> /// 加热 /// </summary> public void Heating() { Console.WriteLine("The food is heating!"); Console.ReadLine(); } } }
希望能对你对事件的理解和使用有所帮助,谢谢!
开博之作,转载麻烦注明原创作者,谢谢。期待下一篇吧:)