理解事件的使用
俺理解的事件就是一个强类型的代理. 在类的内部引发事件,然后调用“宇宙”内的一个方法(它并不知道来自于哪进而),并传进去一个参数,告诉它究竟发生了什么.
所有关心这个事件的对象可以订阅该事件,并把相应的处理方法绑定到这个事件. 在事件发生后这些方法就会被自动执行,而不需要事件源来手动调用, 事件源也不需要与提供该方法的对象发生关联.
一个实例, 我们在照片产生的时候为它打上一个时间戳, 而PhotoManager自已不想干这个活,把它交给了PhotoMonitor来完成 :)
using System;
using System.Collections.Generic;
using System.Text;
![](/Images/OutliningIndicators/None.gif)
namespace EventTest
{
public class PhotoManager
{
public static event EventHandler<PhotoInsertedEventArgs> PhotoInserted;
![](/Images/OutliningIndicators/InBlock.gif)
public PhotoManager()
{
PhotoManager.PhotoInserted += new EventHandler<PhotoInsertedEventArgs>(PhotoMonitor.AddDate);
}
public void InsertPhoto()
{
Photo photo = new Photo();
photo.Title = "Darkangel";
OnPhotoInserted(photo);
Console.WriteLine("A photo is created!!!");
Console.WriteLine("The created date is : ");
Console.WriteLine(photo.CreateDate);
}
protected static void OnPhotoInserted(Photo p)
{
if (PhotoInserted != null)
{
PhotoInsertedEventArgs e = new PhotoInsertedEventArgs(p);
PhotoInserted(null, e);
}
}
}
public class Photo
{
private string title;
![](/Images/OutliningIndicators/InBlock.gif)
public string Title
{
get { return title; }
set { title = value; }
}
![](/Images/OutliningIndicators/InBlock.gif)
private DateTime createDate;
![](/Images/OutliningIndicators/InBlock.gif)
public DateTime CreateDate
{
get { return createDate; }
set { createDate = value; }
}
}
![](/Images/OutliningIndicators/InBlock.gif)
public class PhotoInsertedEventArgs : EventArgs
{
public PhotoInsertedEventArgs(Photo p)
{
this.insertedPhoto = p;
}
![](/Images/OutliningIndicators/InBlock.gif)
private Photo insertedPhoto;
![](/Images/OutliningIndicators/InBlock.gif)
public Photo InsertedPhoto
{
get { return insertedPhoto; }
set { insertedPhoto = value; }
}
}
public static class PhotoMonitor
{
public static void AddDate(object sender, PhotoInsertedEventArgs e)
{
e.InsertedPhoto.CreateDate = DateTime.Now;
}
}
}
测试代码:
using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using EventTest;
![](/Images/OutliningIndicators/None.gif)
namespace TestLib
{
[TestFixture]
public class EventTestFixture
{
[Test]
public void PhotoInsertedEventTest()
{
PhotoManager manager = new PhotoManager();
manager.InsertPhoto();
Assert.IsTrue(true);
}
}
}
所有关心这个事件的对象可以订阅该事件,并把相应的处理方法绑定到这个事件. 在事件发生后这些方法就会被自动执行,而不需要事件源来手动调用, 事件源也不需要与提供该方法的对象发生关联.
一个实例, 我们在照片产生的时候为它打上一个时间戳, 而PhotoManager自已不想干这个活,把它交给了PhotoMonitor来完成 :)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
测试代码:
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)