zoukankan      html  css  js  c++  java
  • EventHandler 泛型委托 武胜

    EventHandler 泛型委托

    // This example demonstrates the EventHandler<T> delegate.
    
    using System;
    using System.Collections.Generic;
    
    //---------------------------------------------------------
    public class MyEventArgs : EventArgs
    {
        private string msg;
    
        public MyEventArgs( string messageData ) {
            msg = messageData;
        }
        public string Message { 
            get { return msg; } 
            set { msg = value; }
        }
    }
    //---------------------------------------------------------
    public class HasEvent
    {
    // Declare an event of delegate type EventHandler of 
    // MyEventArgs.
    
        public event EventHandler<MyEventArgs> SampleEvent;
    
        public void DemoEvent(string val)
        {
        // Copy to a temporary variable to be thread-safe.
            EventHandler<MyEventArgs> temp = SampleEvent;
            if (temp != null)
                temp(this, new MyEventArgs(val));
        }
    }
    //---------------------------------------------------------
    public class Sample
    {
        public static void Main()
        {
            HasEvent he = new HasEvent();
            he.SampleEvent += 
                       new EventHandler<MyEventArgs>(SampleEventHandler);
            he.DemoEvent("Hey there, Bruce!");
            he.DemoEvent("How are you today?");
            he.DemoEvent("I'm pretty good.");
            he.DemoEvent("Thanks for asking!");
        }
        private static void SampleEventHandler(object src, MyEventArgs mea)
        {
            Console.WriteLine(mea.Message);
        }
    }
    //---------------------------------------------------------
    /*
    This example produces the following results:
    
    Hey there, Bruce!
    How are you today?
    I'm pretty good.
    Thanks for asking!
    
    */
    .NET Framework 2.0
    其他版本
    11(共 16)对本文的评价是有帮助 - 评价此主题

    注意:此委托在 .NET Framework 2.0 版中是新增的。

    表示将处理事件的方法。泛型类型参数指定事件所生成的事件数据的类型。

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

     
    [SerializableAttribute] 
    public delegate void EventHandler<TEventArgs> (
    	Object sender,
    	TEventArgs e
    ) where TEventArgs : EventArgs
    
     
    J# 支持使用泛型类型和方法,但不支持进行新的声明。
    
     
    JScript 支持泛型类型和方法。
    

    参数

    sender

    事件源。

    e

    包含事件数据的 EventArgs

    .NET Framework 中的事件模型基于具有事件委托,该委托将事件与事件处理程序连接。引发事件需要两个元素:

    • 引用向事件提供响应的方法的委托。

    • 保存事件数据的类。

    委托是一个定义签名的类型,即方法的返回值类型和参数列表类型。可以使用委托类型来声明一个变量,该变量可以引用与委托签名相同的所有方法。

    事件处理程序委托的标准签名定义一个没有返回值的方法,其第一个参数的类型为 Object,它引用引发事件的实例,第二个参数从 EventArgs 类型派生,它保存事件数据。如果事件不生成事件数据,则第二个参数只是 EventArgs 的一个实例。否则,第二个参数为从 EventArgs 派生的自定义类型,提供保存事件数据所需的全部字段或属性。

    EventHandler 是一种预定义委托,表示事件的事件处理程序方法,它与事件是否生成事件数据无关。如果事件不生成事件数据,则用 EventArgs 替代泛型类型参数;否则,提供自己的自定义事件数据类型并用该类型替代泛型类型参数。

    使用 EventHandler 的优点在于,如果事件生成事件数据,则无需编写自己的自定义委托代码。此外,.NET Framework 只需一个实现就能支持 EventHandler,这与替代泛型类型参数的事件数据类型无关。

    若要将事件与处理事件的方法关联,请向事件添加委托的实例。除非移除了该委托,否则每当发生该事件时就调用事件处理程序。

    有关事件处理程序委托的更多信息,请参见 事件和委托

    下面的代码示例声明事件数据和使用该事件数据的 EventHandler 泛型委托,并演示如何引发该事件。

     
    // This example demonstrates the EventHandler<T> delegate.
    
    using System;
    using System.Collections.Generic;
    
    //---------------------------------------------------------
    public class MyEventArgs : EventArgs
    {
        private string msg;
    
        public MyEventArgs( string messageData ) {
            msg = messageData;
        }
        public string Message { 
            get { return msg; } 
            set { msg = value; }
        }
    }
    //---------------------------------------------------------
    public class HasEvent
    {
    // Declare an event of delegate type EventHandler of 
    // MyEventArgs.
    
        public event EventHandler<MyEventArgs> SampleEvent;
    
        public void DemoEvent(string val)
        {
        // Copy to a temporary variable to be thread-safe.
            EventHandler<MyEventArgs> temp = SampleEvent;
            if (temp != null)
                temp(this, new MyEventArgs(val));
        }
    }
    //---------------------------------------------------------
    public class Sample
    {
        public static void Main()
        {
            HasEvent he = new HasEvent();
            he.SampleEvent += 
                       new EventHandler<MyEventArgs>(SampleEventHandler);
            he.DemoEvent("Hey there, Bruce!");
            he.DemoEvent("How are you today?");
            he.DemoEvent("I'm pretty good.");
            he.DemoEvent("Thanks for asking!");
        }
        private static void SampleEventHandler(object src, MyEventArgs mea)
        {
            Console.WriteLine(mea.Message);
        }
    }
    //---------------------------------------------------------
    /*
    This example produces the following results:
    
    Hey there, Bruce!
    How are you today?
    I'm pretty good.
    Thanks for asking!
    
    */
    
    

    Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

    .NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

    .NET Framework

    受以下版本支持:2.0

    .NET Compact Framework

    受以下版本支持:2.0

  • 相关阅读:
    intellij idea 将taskRequest.java文件识别为文本文档
    react 学习笔记2
    react 学习笔记1
    webpack4 配置笔记(转自掘金)
    音乐播放之进度条-自定义
    EBS
    Python 学习笔记
    Form 电子表格(JTF GRID)
    Form 中实现历史记录查询
    Form 去掉使用格式掩码带来的多余字符
  • 原文地址:https://www.cnblogs.com/zeroone/p/3131676.html
Copyright © 2011-2022 走看看