使用英语阅读

通过


EventHandler<TEventArgs> 委托

定义

表示当事件提供数据时将处理该事件的方法。

C#
public delegate void EventHandler<TEventArgs>(object sender, TEventArgs e);
C#
public delegate void EventHandler<TEventArgs>(object? sender, TEventArgs e);
C#
[System.Serializable]
public delegate void EventHandler<TEventArgs>(object sender, TEventArgs e) where TEventArgs : EventArgs;
C#
[System.Serializable]
public delegate void EventHandler<TEventArgs>(object sender, TEventArgs e);

类型参数

TEventArgs

事件生成的事件数据的类型。

参数

sender
Object

事件源。

e
TEventArgs

包含事件数据的对象。

属性

示例

以下示例演示名为 的事件 ThresholdReached。 事件与 EventHandler<TEventArgs> 委托相关联。

C#
using System;

namespace ConsoleApplication3
{
    public class Program3
    {
        public static void Main()
        {
            Counter c = new(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 readonly int _threshold;
        private int _total;

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

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

        protected virtual void OnThresholdReached(ThresholdReachedEventArgs e)
        {
            ThresholdReached?.Invoke(this, e);
        }

        public event EventHandler<ThresholdReachedEventArgs>? ThresholdReached;
    }

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

注解

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

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

  • (可选)如果事件提供数据,则为保存事件数据的类。

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

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

委托 EventHandler<TEventArgs> 是一个预定义的委托,表示生成数据的事件的事件处理程序方法。 使用 EventHandler<TEventArgs> 的优点是,如果事件生成事件数据,则无需编写自己的自定义委托代码。 只需提供事件数据对象的类型作为泛型参数。

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

有关事件处理程序委托的详细信息,请参阅 处理和引发事件

扩展方法

GetMethodInfo(Delegate)

获取指示指定委托表示的方法的对象。

适用于

产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

另请参阅