登录  
 加关注
查看详情
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

学无止境

一点积累,与大家分享

 
 
 

日志

 
 

EventArgs 类  

2010-05-01 13:54:22|  分类: C#技术 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
from:http://msdn.microsoft.com/zh-cn/library/system.eventargs%28v=VS.80%29.aspx

EventArgs 是包含事件数据的类的基类。

命名空间:System
程序集:mscorlib(在 mscorlib.dll 中)

语法

[SerializableAttribute] 
[ComVisibleAttribute(true)]
public class EventArgs
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */
public class EventArgs
备注

此类不包含事件数据,在事件引发时不向事件处理程序传递状态信息的事件会使用此类。如果事件处理程序需要状态信息,则应用程序必须从此类派生一个类来保存数据。

例如,System.AssemblyLoadEventArgs 类用于保存程序集加载事件的数据,并包含描述所加载程序集的 System.Reflection.Assembly

有关事件的更多信息,请参见 EventHandler 主题。

示例

下面的代码阐释了如何使用 EventArgs

在此示例中,FireEventArgs 是从 EventArgs 派生的事件参数集,它在调用 ActivateFireAlarm 引发事件时被传递到 FireEventHandler。

using System;

// FireEventArgs: a custom event inherited from EventArgs.

public class FireEventArgs: EventArgs {
public FireEventArgs(string room, int ferocity) {
this.room = room;
this.ferocity = ferocity;
}

// The fire event will have two pieces of information--
// 1) Where the fire is, and 2) how "ferocious" it is.

public string room;
public int ferocity;

} //end of class FireEventArgs


// Class with a function that creates the eventargs and initiates the event
public class FireAlarm {

// Events are handled with delegates, so we must establish a FireEventHandler
// as a delegate:
//定义函数指针 define
public delegate void FireEventHandler(object sender, FireEventArgs fe);

// Now, create a public event "FireEvent" whose type is our FireEventHandler delegate.
//创建函数指针 new event 类型的函数
public event FireEventHandler FireEvent;

// This will be the starting point of our event-- it will create FireEventArgs,
// and then raise the event, passing FireEventArgs.

public void ActivateFireAlarm(string room, int ferocity) {

FireEventArgs fireArgs = new FireEventArgs(room, ferocity);

// Now, raise the event by invoking the delegate. Pass in
// the object that initated the event (this) as well as FireEventArgs.
// The call must match the signature of FireEventHandler.
//调用函数指针 call
FireEvent(this, fireArgs);
}
} // end of class FireAlarm


// Class which handles the event

class FireHandlerClass {

// Create a FireAlarm to handle and raise the fire events.

public FireHandlerClass(FireAlarm fireAlarm) {

// Add a delegate containing the ExtinguishFire function to the class'
// event so that when FireAlarm is raised, it will subsequently execute
// ExtinguishFire.
//订阅者 订阅函数
//new delegate 函数 ,函数名为ExtinguishFire,参数类型与 public delegate void FireEventHandler(object sender, FireEventArgs fe)
 //的参数类型一致
fireAlarm.FireEvent += new FireAlarm.FireEventHandler(ExtinguishFire);

}

// This is the function to be executed when a fire event is raised.


 void ExtinguishFire(object sender, FireEventArgs fe) {

Console.WriteLine("\nThe ExtinguishFire function was called by {0}.", sender.ToString());

// Now, act in response to the event.
//将发布者消息 发送给订阅者
if (fe.ferocity < 2)
Console.WriteLine("This fire in the {0} is no problem. I'm going to pour some water on it.", fe.room);
else if (fe.ferocity < 5)
Console.WriteLine("I'm using FireExtinguisher to put out the fire in the {0}.", fe.room);
else
Console.WriteLine("The fire in the {0} is out of control. I'm calling the fire department!", fe.room);
}
} //end of class FireHandlerClass

public class FireEventTest {
public static void Main () {

// Create an instance of the class that will be firing an event.

FireAlarm myFireAlarm = new FireAlarm();

// Create an instance of the class that will be handling the event. Note that
// it receives the class that will fire the event as a parameter.

FireHandlerClass myFireHandler = new FireHandlerClass(myFireAlarm);

//use our class to raise a few events and watch them get handled
myFireAlarm.ActivateFireAlarm("Kitchen", 3);
myFireAlarm.ActivateFireAlarm("Study", 1);
myFireAlarm.ActivateFireAlarm("Porch", 5);

return;

} //end of main

} // end of FireEventTest
事件(C# 编程指南):
http://msdn.microsoft.com/zh-cn/library/awbftdfh%28v=VS.80%29.aspx
如何:订阅和取消订阅事件
利用delegate实现订发布者数据传递给订阅者;订阅者也可回调发布者;实现两者之间数据通讯;
http://msdn.microsoft.com/zh-cn/library/ms366768%28v=VS.80%29.aspx
订户类需要引用发行者类才能订阅其事件

如何:实现接口事件:
http://msdn.microsoft.com/zh-cn/library/ak9w5846%28v=VS.80%29.aspx
事件 是一个属性并非方法;用{add,remove 来执行订阅},调用时候 属性用 += 订阅操作



  评论这张
 
阅读(1144)| 评论(0)

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018