HOW TO:在您的類別中實作事件
更新:2007 年 11 月
下列程序說明如何在類別中實作事件:第一個程序會實作沒有關聯資料的事件,它使用 System.EventArgs 和 System.EventHandler 類別做為事件資料和委派處理常式。第二個程序會實作具有自訂資料的事件,它會定義自訂類別做為事件資料和事件委派處理常式。
如需說明引發和處理事件的完整範例,請參閱 HOW TO:引發和使用事件。
若要實作不含事件特定資料的事件
在類別中定義公用事件成員。將此事件成員的型別設定為 System.EventHandler 委派。
public class Countdown { ... public event EventHandler CountdownCompleted; }
Public Class Countdown ... Public Event CountdownCompleted As EventHandler End Class
在引發事件的類別中提供一個保護的方法。將此方法命名為 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
決定何時要在您的類別中引發此事件。呼叫 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
若要實作具有事件特定資料的事件
定義提供事件資料的類別。將此類別命名為 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
宣告事件的委派。將委派命名為 EventNameEventHandler。
public delegate void AlarmEventHandler(object sender, AlarmEventArgs e);
Public Delegate Sub AlarmEventHandler(sender As Object, e As AlarmEventArgs)
定義類別中名為 EventName 的公用事件成員。將此事件成員的型別設定為事件委派型別。
public class AlarmClock { ... public event AlarmEventHandler Alarm; }
Public Class AlarmClock ... Public Event Alarm As AlarmEventHandler End Class
在引發事件的類別中定義一個保護的方法。將此方法命名為 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
決定何時要在您的類別中引發此事件。呼叫 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