EventHandler<TEventArgs> EventHandler<TEventArgs> EventHandler<TEventArgs> EventHandler<TEventArgs> Delegate

定义

表示将在事件提供数据时处理该事件的方法。Represents the method that will handle an event when the event provides data.

generic <typename TEventArgs>
public delegate void EventHandler(System::Object ^ sender, TEventArgs e);
[System.Serializable]
public delegate void EventHandler<TEventArgs>(object sender, TEventArgs e);
type EventHandler<'EventArgs> = delegate of obj * 'EventArgs -> unit
Public Delegate Sub EventHandler(Of TEventArgs)(sender As Object, e As TEventArgs)

类型参数

TEventArgs

由该事件生成的事件数据的类型。The type of the event data generated by the event.

参数

sender
Object Object Object Object

事件源。The source of the event.

e

包含事件数据的对象。An object that contains the event data.

继承
EventHandler<TEventArgs>EventHandler<TEventArgs>EventHandler<TEventArgs>EventHandler<TEventArgs>
属性

示例

下面的示例演示名为的事件ThresholdReachedThe following example shows an event named ThresholdReached. 与事件关联EventHandler<TEventArgs>委托。The event is associated with an EventHandler<TEventArgs> delegate.

using namespace System;

public ref class ThresholdReachedEventArgs : public EventArgs
{
   public:
      property int Threshold;
      property DateTime TimeReached;
};

public ref class Counter
{
   private:
      int threshold;
      int total;

   public:
      Counter() {};

      Counter(int passedThreshold)
      {
         threshold = passedThreshold;
      }

      void Add(int x)
      {
          total += x;
          if (total >= threshold) {
             ThresholdReachedEventArgs^ args = gcnew ThresholdReachedEventArgs();
             args->Threshold = threshold;
             args->TimeReached = DateTime::Now;
             OnThresholdReached(args);
          }
      }

      event EventHandler<ThresholdReachedEventArgs^>^ ThresholdReached;

   protected:
      virtual void OnThresholdReached(ThresholdReachedEventArgs^ e)
      {
         ThresholdReached(this, e);
      }
};

public ref class SampleHandler
{
   public:
      static void c_ThresholdReached(Object^ sender, ThresholdReachedEventArgs^ e)
      {
         Console::WriteLine("The threshold of {0} was reached at {1}.",
                            e->Threshold,  e->TimeReached);
         Environment::Exit(0);
      }
};

void main()
{
   Counter^ c = gcnew Counter((gcnew Random())->Next(10));
   c->ThresholdReached += gcnew EventHandler<ThresholdReachedEventArgs^>(SampleHandler::c_ThresholdReached);

   Console::WriteLine("press 'a' key to increase total");
   while (Console::ReadKey(true).KeyChar == 'a') {
      Console::WriteLine("adding one");
      c->Add(1);
   }
}
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Counter c = new Counter(new Random().Next(10));
            c.ThresholdReached += c_ThresholdReached;

            Console.WriteLine("press 'a' key to increase total");
            while (Console.ReadKey(true).KeyChar == 'a')
            {
                Console.WriteLine("adding one");
                c.Add(1);
            }
        }

        static void c_ThresholdReached(object sender, ThresholdReachedEventArgs e)
        {
            Console.WriteLine("The threshold of {0} was reached at {1}.", e.Threshold,  e.TimeReached);
            Environment.Exit(0);
        }
    }

    class Counter
    {
        private int threshold;
        private int total;

        public Counter(int passedThreshold)
        {
            threshold = passedThreshold;
        }

        public void Add(int x)
        {
            total += x;
            if (total >= threshold)
            {
                ThresholdReachedEventArgs args = new ThresholdReachedEventArgs();
                args.Threshold = threshold;
                args.TimeReached = DateTime.Now;
                OnThresholdReached(args);
            }
        }

        protected virtual void OnThresholdReached(ThresholdReachedEventArgs e)
        {
            EventHandler<ThresholdReachedEventArgs> handler = ThresholdReached;
            if (handler != null)
            {
                handler(this, e);
            }
        }

        public event EventHandler<ThresholdReachedEventArgs> ThresholdReached;
    }

    public class ThresholdReachedEventArgs : EventArgs
    {
        public int Threshold { get; set; }
        public DateTime TimeReached { get; set; }
    }
}
Module Module1

    Sub Main()
        Dim c As Counter = New Counter(New Random().Next(10))
        AddHandler c.ThresholdReached, AddressOf c_ThresholdReached

        Console.WriteLine("press 'a' key to increase total")
        While Console.ReadKey(True).KeyChar = "a"
            Console.WriteLine("adding one")
            c.Add(1)
        End While
    End Sub

    Sub c_ThresholdReached(sender As Object, e As ThresholdReachedEventArgs)
        Console.WriteLine("The threshold of {0} was reached at {1}.", e.Threshold, e.TimeReached)
        Environment.Exit(0)
    End Sub
End Module

Class Counter
    Private threshold As Integer
    Private total As Integer

    Public Sub New(passedThreshold As Integer)
        threshold = passedThreshold
    End Sub

    Public Sub Add(x As Integer)
        total = total + x
        If (total >= threshold) Then
            Dim args As ThresholdReachedEventArgs = New ThresholdReachedEventArgs()
            args.Threshold = threshold
            args.TimeReached = DateTime.Now
            OnThresholdReached(args)
        End If
    End Sub

    Protected Overridable Sub OnThresholdReached(e As ThresholdReachedEventArgs)
        RaiseEvent ThresholdReached(Me, e)
    End Sub

    Public Event ThresholdReached As EventHandler(Of ThresholdReachedEventArgs)
End Class

Class ThresholdReachedEventArgs
    Inherits EventArgs

    Public Property Threshold As Integer
    Public Property TimeReached As DateTime
End Class

注解

.NET Framework 中的事件模型基于具有其处理程序与连接事件的事件委托。The event model in the .NET Framework is based on having an event delegate that connects an event with its handler. 若要引发事件,需要两个元素:To raise an event, two elements are needed:

  • 提供对事件作出响应指的是一种方法的委托。A delegate that refers to a method that provides the response to the event.

  • (可选) 一个类,包含事件数据,如果事件提供数据。Optionally, a class that holds the event data, if the event provides data.

委托是一种定义一个签名,也就是说,返回值类型和方法的参数列表类型。The delegate is a type that defines a signature, that is, the return value type and parameter list types for a method. 可以使用的委托类型来声明变量可以引用具有相同的签名与委托的任何方法。You can use the delegate type to declare a variable that can refer to any method with the same signature as the delegate.

事件处理程序委托的标准签名定义不返回值的方法。The standard signature of an event handler delegate defines a method that does not return a value. 此方法的第一个参数的类型是Object和引用引发事件的实例。This method's first parameter is of type Object and refers to the instance that raises the event. 第二个参数从类型派生EventArgs和保存事件数据。Its second parameter is derived from type EventArgs and holds the event data. 如果该事件不会生成事件数据,第二个参数是只需值EventArgs.Empty字段。If the event does not generate event data, the second parameter is simply the value of the EventArgs.Empty field. 否则,第二个参数是派生自类型EventArgs和提供的任何字段或保存事件数据所需的属性。Otherwise, the second parameter is a type derived from EventArgs and supplies any fields or properties needed to hold the event data.

EventHandler<TEventArgs>委托是一个预定义的委托,表示生成数据的事件的事件处理程序方法。The EventHandler<TEventArgs> delegate is a predefined delegate that represents an event handler method for an event that generates data. 使用的优点EventHandler<TEventArgs>是你不需要代码自定义委托,如果您的活动生成的事件数据。The advantage of using EventHandler<TEventArgs> is that you do not need to code your own custom delegate if your event generates event data. 只需作为泛型参数提供的事件数据对象的类型。You simply provide the type of the event data object as the generic parameter.

若要将事件与将处理的事件的方法相关联,添加到事件的委托的实例。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.

有关事件处理程序委托的详细信息,请参阅处理和引发事件For more information about event handler delegates, see Handling and Raising Events.

扩展方法

GetMethodInfo(Delegate) GetMethodInfo(Delegate) GetMethodInfo(Delegate) GetMethodInfo(Delegate)

获取指示指定委托表示的方法的对象。Gets an object that represents the method represented by the specified delegate.

适用于

另请参阅