消息与事件
namespace ControlDelegate
{
/// <summary>
/// 事件发送者,可以触发事件
/// </summary>
public delegate void EventSenderEventHandler(string strText);
public class EventSender:System.EventArgs
{
public EventSender()
{
//相当于一个安纽类,这在WINFORM中
//类的具体是不用我们实现的了,我们只管继承就可以了
}
public event EventSenderEventHandler TextOut;
public void OnTextOut()
{
if(null!=TextOut)
{
TextOut("事件点火了");
return;
}
Console.WriteLine("事件没被预定");
}
}
/// <summary>
/// 事件的接收者,捕捉事件,并做出响应
/// </summary>
public class EventReceiver
{
public EventReceiver()
{
//我们通常编的WINFORM程序好象都是做为消息接收者程序
//而消息发送者该是WINDOWS本身吧
}
public static void Main()
{
EventSender sender=new EventSender();//相当于一个安纽类
sender.TextOut+=new EventSenderEventHandler(EventReceiver.StaticCatchEvent);
sender.OnTextOut();
sender.TextOut-=new EventSenderEventHandler(EventReceiver.StaticCatchEvent);
sender.OnTextOut();
EventReceiver receiver=new EventReceiver();
sender.TextOut+=new EventSenderEventHandler(receiver.InstanceCatchEvent);
sender.OnTextOut();//相当于我们点击某个按纽吧
Console.ReadLine();
}