EventArgs 类 
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
表示包含事件数据的类的基类,并提供用于不包含事件数据的事件的值。
public ref class EventArgspublic class EventArgs[System.Serializable]
public class EventArgs[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public class EventArgstype EventArgs = class[<System.Serializable>]
type EventArgs = class[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type EventArgs = classPublic Class EventArgs- 继承
- 
				EventArgs
- 派生
- 属性
示例
以下示例演示派生自 EventArgs 类的名为 ThresholdReachedEventArgs 的自定义事件数据类。 事件数据类的实例将传递给事件的事件处理程序 ThresholdReached 。
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 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
注解
此类用作表示事件数据的所有类的基类。 例如, System.AssemblyLoadEventArgs 类派生自 EventArgs ,用于保存程序集加载事件的数据。 若要创建自定义事件数据类,请创建派生自 类 EventArgs 的类,并提供用于存储必要数据的属性。 自定义事件数据类的名称应以 结尾 EventArgs。
若要传递不包含任何数据的对象,请使用 Empty 字段。
有关事件的详细信息,请参阅 处理和引发事件 一文。
构造函数
| EventArgs() | 初始化 EventArgs 类的新实例。 | 
字段
| Empty | 提供要用于没有事件数据的事件的值。 | 
方法
| Equals(Object) | 确定指定对象是否等于当前对象。(继承自 Object) | 
| GetHashCode() | 作为默认哈希函数。(继承自 Object) | 
| GetType() | 获取当前实例的 Type。(继承自 Object) | 
| MemberwiseClone() | 创建当前 Object 的浅表副本。(继承自 Object) | 
| ToString() | 返回表示当前对象的字符串。(继承自 Object) |