触发事件,检查是否注册过事件的方法
2015-01-12 09:16
369 查看
下面的代码,会发现事件没有被注册
修改第四步的代码,先将事件复制到临时变量里面,然后通过这个临时变量来处理,就不会有问题
class Program { static void Main(string[] args) { EventCheck check = new EventCheck(); check.OnDataArrived(new DataArrivedEventArgs() { data="Hello World!"}); Console.ReadKey(); } static void check_DataArrived(object sender, DataArrivedEventArgs e) { Console.WriteLine(e.data); } } class EventCheck { //第三步 use the delegate type to declare a variable that can refer to any method with the same signature as the delegate. //To associate the event with the method that will handle the event, add an instance of the delegate to the event. //The event handler is called whenever the event occurs, unless you remove the delegate. internal event DataArrivedEventHandler DataArrived; //第四步 internal void OnDataArrived(DataArrivedEventArgs e) { try { //DataArrivedEventHandler handler = DataArrived; //if (handler != null) //{ // handler(this, e); //} DataArrived += EventCheck_DataArrived; if (DataArrived != null) { DataArrived -= EventCheck_DataArrived; DataArrived(this, e); } } catch (Exception ex) { Console.WriteLine("遇到异常{0}", ex.Message); } } void EventCheck_DataArrived(object sender, DataArrivedEventArgs e) { Console.WriteLine(e.data); } } //第二步 Represents the method that will handle an event when the event provides data. //This method's first parameter is of type Object and refers to the instance that raises the event. //Its second parameter is derived from type EventArgs and holds the event data. //If the event does not generate event data, the second parameter is simply the value of the EventArgs.Empty field. //Otherwise, the second parameter is a type derived from EventArgs and supplies any fields or properties needed to hold the event data. delegate void DataArrivedEventHandler(object sender,DataArrivedEventArgs e); //正式写代码的时候,可以使用EventHandler<TEventArgs>泛型委托 //第一步 //create a custom event data class, create a class that derives from the EventArgs class and provide the properties to store the necessary data. //The name of your custom event data class should end with EventArgs. class DataArrivedEventArgs : EventArgs { internal string data; }
修改第四步的代码,先将事件复制到临时变量里面,然后通过这个临时变量来处理,就不会有问题
//第四步 internal void OnDataArrived(DataArrivedEventArgs e) { try { //DataArrivedEventHandler handler = DataArrived; //if (handler != null) //{ // handler(this, e); //} DataArrived += EventCheck_DataArrived; DataArrivedEventHandler handler = DataArrived; if (handler != null) { DataArrived -= EventCheck_DataArrived; handler(this, e); } } catch (Exception ex) { Console.WriteLine("遇到异常{0}", ex.Message); } }
相关文章推荐
- jQuery检查事件是否触发的方法
- jQuery检查事件是否触发的方法
- 作个笔记 C#检查事件是否已经被绑定的方法
- dedeCMS 注册失败,请检查资料是否有误或与管理员联系-解决方法
- 多次注册事件会导致一个事件被触发多次的解决方法
- 回发或回调参数无效。在配置中使用 或在页面中使用 启用了事件验证。出于安全目的,此功能验证回发或回调事件的参数是否来源于最初呈现这些事件的服务器控件。如果数据有效并且是预期的,则使用 ClientScriptManager.RegisterForEventValidation 方法来注册回发或回调数据以进行验证。
- 多次注册事件会导致一个事件被触发多次的解决方法
- click多次注册事件会导致一个事件被触发多次的解决方法
- 回发或回调参数无效。在配置中使用 或在页面中使用 启用了事件验证。出于安全目的,此功能验证回发或回调事件的参数是否来源于最初呈现这些事件的服务器控件。如果数据有效并且是预期的,则使用 ClientScriptManager.RegisterForEventValidation 方法来注册回发或回调数据以进行验证。
- 回发或回调参数无效。在配置中使用 或在页面中使用 启用了事件验证。出于安全目的,此功能验证回发或回调事件的参数是否来源于最初呈现这些事件的服务器控件。如果数据有效并且是预期的,则使用 ClientScriptManager.RegisterForEventValidation 方法来注册回发或回调数据以进行验证。
- 关于serv-u公网访问出现“打开ftp服务器上的文件夹时发生错误。请检查是否有权限访问该文件夹。详细信息:操作超时”的解决方法
- OCX控件注册相关(检查是否注册,注册,反注册)
- PHP 中检查库或函数是否可用的方法
- 检查两个数据库里的表名、字段是否一致的一种方法
- 检查两个数据库里的表名、字段是否一致的一种方法
- 关于serv-u公网访问出现“打开ftp服务器上的文件夹时发生错误。请检查是否有权限访问该文件夹。详细信息:操作超时”的解决方法
- c#如何取得事件注册的方法
- js在insertRow里面触发事件的方法
- 判断是否首次触发pageLoad 与 PageRequestManager.getInstance()对象的几个事件触发顺序
- addEventListener注册事件进行传递参数的方法