EventHandler<TEventArgs> 代理人

定義

代表當事件提供資料時,該事件會處理該事件的方法。

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

類型參數

TEventArgs

事件所產生的事件資料類型。

參數

sender
Object

事件的 來源。

e
TEventArgs

一個包含事件資料的物件。

屬性

範例

以下範例顯示一個名為 ThresholdReached的事件。 該活動與代表 EventHandler<TEventArgs> 相關聯。

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; }
    }
}
open System

type ThresholdReachedEventArgs(threshold, timeReached) =
    inherit EventArgs()
    member _.Threshold = threshold
    member _.TimeReached = timeReached

type Counter(threshold) =
    let mutable total = 0

    let thresholdReached = Event<_>()

    member this.Add(x) =
        total <- total + x
        if total >= threshold then
            let args = ThresholdReachedEventArgs(threshold, DateTime.Now)
            thresholdReached.Trigger(this, args)

    [<CLIEvent>]
    member _.ThresholdReached = thresholdReached.Publish

let c_ThresholdReached(sender, e: ThresholdReachedEventArgs) =
    printfn $"The threshold of {e.Threshold} was reached at {e.TimeReached}."
    exit 0

let c = Counter(Random().Next 10)
c.ThresholdReached.Add c_ThresholdReached

printfn "press 'a' key to increase total"
while Console.ReadKey(true).KeyChar = 'a' do
    printfn "adding one"
    c.Add 1
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 中的事件模型基於一個事件代理,將事件與其處理器連結起來。 要引發事件,需要兩個要素:

  • 代表指的是對事件做出回應的方法。
  • 可選擇性地,若事件提供資料,則可選擇一個類別來儲存事件資料。

代理是一種定義簽名的型別,也就是方法的回傳值型別和參數列表型別。 你可以使用 delegate 類型宣告一個變數,該變數可以指向與代理擁有相同簽名的任何方法。

事件處理代理的標準簽章定義了一個不會回傳值的方法。 此方法的第一個參數為型別 Object ,指的是引發事件的實例。 第二個參數由型別 EventArgs 推導,並儲存事件資料。 若事件不產生事件資料,第二個參數即為欄位值 EventArgs.Empty 。 否則,第二個參數是從中衍生出 EventArgs 的型別,提供任何欄位或屬性以保存事件資料。

EventHandler<TEventArgs>代理是一個預先定義的代理,代表一個用於產生資料的事件處理方法。 使用該系統 EventHandler<TEventArgs> 的優點是,如果你的事件產生事件資料,你不需要自己寫自訂代理程式。 你只要提供事件資料物件的類型作為通用參數即可。

要將事件與處理該事件的方法關聯,請將代理的實例加入事件中。 事件處理程序會在事件發生時被呼叫,除非你移除代理。

欲了解更多關於事件處理代理的資訊,請參閱 「處理與提升事件」。

擴充方法

名稱 Description
GetMethodInfo(Delegate)

取得一個代表指定代理所代表方法的物件。

適用於

另請參閱