public delegate void DataInputEventHandler(object sender,EventArgs e);//如果你不重载EventArgs,此处不必做委托,用Event就可以了。
public class EventSource()
{
public event DataInputEventHandler DataInputEvent;
private void OnDataInputEvent(EventArgs e)
{
if(this.DataInputEvent!=null)//必须判断是否为null,否则如果事件监视方不监视此事件,则程序会出错
{
this.DataInputEvent(this,e);
}
}
public void AMethod()
{
//do something!
...
this.OnDataInputEvent(new EventArgs());//激活事件;
}
}
public class EventTarget()
{
private EventSource source;
public EventTarget()
{
this.source=new EventSource();
source.DataInputEvent+=new DataInputEventHandler(this.DoThing);
}
protected void DoThing(object sender,EventArgs)
{
MessageBox.Show("开心警告:有数据录入了!");
}
}
}
就这么简单
头一次手写这么长的代码,真累:(
80酷酷网 80kuku.com