如何:在类中实现事件

更新:2007 年 11 月

下面的过程说明如何在类中实现事件。第一个过程实现没有关联数据的事件,它将 System.EventArgs 类和 System.EventHandler 类用作事件数据和委托处理程序。第二个过程实现具有自定义数据的事件,它为事件数据和事件委托处理程序定义自定义类。

有关阐释引发和处理事件的完整示例,请参见如何:引发和使用事件

实现不具有事件特定的数据的事件

  1. 在类中定义公共事件成员。将事件成员的类型设置为 System.EventHandler 委托。

    public class Countdown 
    {
        ...
        public event EventHandler CountdownCompleted;   
    }
    
    Public Class Countdown
        ...
        Public Event CountdownCompleted As EventHandler
    End Class
    
  2. 在引发事件的类中提供一个受保护的方法。对 OnEventName 方法进行命名。在该方法中引发该事件。

    public class Countdown 
    {
        ...
        public event EventHandler CountdownCompleted;   
        protected virtual void OnCountdownCompleted(EventArgs e)    {        if (CountdownCompleted != null)            CountdownCompleted(this, e);    }
    }
    
    Public Class Countdown
       ...
       Public Event CountdownCompleted As EventHandler
       Protected Overridable Sub OnCountdownCompleted(e As EventArgs)      RaiseEvent CountdownCompleted(Me, e)   End Sub
    End Class
    
  3. 在类中确定引发该事件的时间。调用 OnEventName 以引发该事件。

    public class Countdown 
    {
        ...
        public void Decrement
        {
            internalCounter = internalCounter - 1;
            if (internalCounter == 0)
                OnCountdownCompleted(new EventArgs());
        }
    }
    
    Public Class Countdown
        ...
        Public Function Decrement
            InternalCounter = internalCounter - 1
            If internalCounter = 0
                OnCountdownComplete(New EventArgs())
            End If
        End Function
    End Class
    

实现具有事件特定的数据的事件

  1. 定义一个提供事件数据的类。对类 EventNameArgs 进行命名,从 System.EventArgs 派生该类,然后添加所有事件特定的成员。

    public class AlarmEventArgs : EventArgs 
    {
       private readonly int nrings = 0;
       private readonly bool snoozePressed = false;
    
       //Constructor.
       public AlarmEventArgs(bool snoozePressed, int nrings) 
       {
          this.snoozePressed = snoozePressed;
          this.nrings = nrings;
       }
    
       //Properties.
       public string AlarmText {  
          ...
       }
       public int NumRings {
          ...
       }
       public bool SnoozePressed{
          ...
       }
    }
    
    Public Class AlarmEventArgs
       Inherits EventArgs
       Private nrings As Integer = 0
       Private _snoozePressed As Boolean = False
    
       'Constructor.
       Public Sub New(ByVal snoozePressed As Boolean, ByVal nrings As Integer) 
          Me.snoozePressed = snoozePressed
          Me.nrings = nrings
       End Sub
    
       'Properties.
       Public ReadOnly Property AlarmText() As String
          ...
       End Property 
    
       Public ReadOnly Property NumRings() As Integer
          ...
       End Property 
    
       Public ReadOnly Property SnoozePressed() As Boolean
          ...
       End Property
    End Class
    
  2. 声明事件的委托。对委托 EventNameEventHandler 进行命名。

    public delegate void AlarmEventHandler(object sender, AlarmEventArgs e);
    
    Public Delegate Sub AlarmEventHandler(sender As Object, e As AlarmEventArgs)
    
  3. 在类中定义名为 EventName 的公共事件成员。将事件成员的类型设置为事件委托类型。

    public class AlarmClock 
    {
        ...
        public event AlarmEventHandler Alarm;
    }
    
    Public Class AlarmClock
        ...
        Public Event Alarm As AlarmEventHandler
    End Class
    
  4. 在引发事件的类中定义一个受保护的方法。对 OnEventName 方法进行命名。在该方法中引发该事件。

    public class AlarmClock 
    {
        ...
        public event AlarmHandler Alarm;
        protected virtual void OnAlarm(AlarmEventArgs e)    {      if (Alarm != null)           Alarm(this, e);     }
    }
    
    Public Class AlarmClock
        ...
        Public Event Alarm As AlarmEventHandler
        Protected Overridable Sub OnAlarm(e As AlarmEventArgs)        RaiseEvent Alarm(Me, e)    End Sub
    End Class
    
  5. 在类中确定引发该事件的时间。调用 OnEventName 以引发该事件并使用 EventNameEventArgs 传入事件特定的数据。

    Public Class AlarmClock
    {
        ...
        public void Start()
        {
            ...
            System.Threading.Thread.Sleep(300);
            AlarmEventArgs e = new AlarmEventArgs(false, 0);
            OnAlarm(e);
        }
    }
    
    Public Class AlarmClock
        ...
        Public Function Start
            ...
            System.Threading.Thread.Sleep(300)
            Dim e As AlarmEventArgs = New AlarmEventArgs(False, 0)
            OnAlarm(e)
        End Function
    End Class
    

请参见

任务

如何:引发和使用事件

概念

事件和委托

引发事件

其他资源

处理和引发事件