C# 編程指南
如何:實現介面事件(C# 編程指南)
介面可聲明事件。下面的樣本示範如何在類中實現介面事件。介面事件的實現規則與任何介面方法或屬性的實現規則基本相同。在類中實現介面事件在類中聲明事件,然後在適當的位置調用該事件。 複製代碼public interface IDrawingObject{ event EventHandler ShapeChanged;}public class MyEventArgs : EventArgs {…}public class Shape : IDrawingObject{ event EventHandler ShapeChanged; void ChangeShape() { // Do something before the event… OnShapeChanged(new MyEventsArgs(…)); // or do something after the event. } protected virtual void OnShapeChanged(MyEventArgs e) { if(ShapeChanged != null) { ShapeChanged(this, e); } }}樣本下面的樣本示範如何處理以下的不常見情況:您的類是從兩個以上的介面繼承的,每個介面都含有同名事件)。在這種情況下,您至少要為其中一個事件提供明確介面實作。為事件編寫明確介面實作時,必須編寫
add 和
remove 事件訪問器。這兩個事件訪問器通常由編譯器提供,但在這種情況下編譯器不能提供。您可以提供自己的訪問器,以便指定這兩個事件是由您的類中的同一事件表示,還是由不同事件表示。例如,根據介面規範,如果事件應在不同時間引發,則可以將每個事件與類中的一個單獨實現關聯。在下面的樣本中,訂戶將形狀引用強制轉換為 IShape 或 IDrawingObject,從而確定自己將會接收哪個 OnDraw 事件。 C#複製代碼namespace WrapTwoInterfaceEvents{ using System; public interface IDrawingObject { // Raise this event before drawing // the object. event EventHandler OnDraw; } public interface IShape { // Raise this event after drawing // the shape. event EventHandler OnDraw; } // Base class event publisher inherits two // interfaces, each with an OnDraw event publicclass Shape : IDrawingObject, IShape { // Create an event for each interface event event EventHandler PreDrawEvent; event EventHandler PostDrawEvent; // Explicit interface implementation required. // Associate IDrawingObject's event with // PreDrawEvent event EventHandler IDrawingObject.OnDraw { add { PreDrawEvent += value; } remove { PreDrawEvent -= value; } } // Explicit interface implementation required. // Associate IShape's event with // PostDrawEvent event EventHandler IShape.OnDraw { add { PostDrawEvent += value; } remove { PostDrawEvent -= value; } } // For the sake of simplicity this one method // implements both interfaces. publicvoid Draw() { // Raise IDrawingObject's event before the object is drawn. EventHandler handler = PreDrawEvent; if (handler != null) { handler(this, new EventArgs()); } Console.WriteLine("Drawing a shape."); // RaiseIShape's event after the object is drawn. handler = PostDrawEvent; if (handler != null) { handler(this, new EventArgs()); } } } publicclass Subscriber1 { // References the shape object as an IDrawingObject public Subscriber1(Shape shape) { IDrawingObject d = (IDrawingObject)shape; d.OnDraw += new EventHandler(d_OnDraw); } void d_OnDraw(object sender, EventArgs e) { Console.WriteLine("Sub1 receives the IDrawingObject event."); } } // References the shape object as an IShape publicclass Subscriber2 { public Subscriber2(Shape shape) { IShape d = (IShape)shape; d.OnDraw += new EventHandler(d_OnDraw); } void d_OnDraw(object sender, EventArgs e) { Console.WriteLine("Sub2 receives the IShape event."); } } publicclass Program { staticvoid Main(string[] args) { Shape shape = new Shape(); Subscriber1 sub = new Subscriber1(shape); Subscriber2 sub2 = new Subscriber2(shape); shape.Draw(); Console.WriteLine("Press Enter to close this window."); Console.ReadLine(); } } }輸出Sub1 receives the IDrawingObject event.Drawing a shape.Sub2 receives the IShape event. (來源:msdn )