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