您好,登錄后才能下訂單哦!
這篇文章主要介紹“如何利用C#實現接口事件”,在日常操作中,相信很多人在如何利用C#實現接口事件問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”如何利用C#實現接口事件”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
C#接口事件的實現是如何的呢?下面的C#接口事件示例演示如何在類中實現接口事件。實現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); } } }
C#接口事件示例
下面的示例演示如何處理以下的不常見情況:您的類是從兩個以上的接口繼承的,每個接口都含有同名事件)。在這種情況下,您至少要為其中一個事件提供顯式接口實現。為事件編寫顯式接口實現時,必須編寫 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 public class Shape : IDrawingObject, IShape { // Create an event for each interface event event EventHandler PreDrawEvent; event EventHandler PostDrawEvent; object objectLock = new Object(); // Explicit interface implementation required. // Associate IDrawingObject's event with // PreDrawEvent event EventHandler IDrawingObject.OnDraw { add { lock (objectLock) { PreDrawEvent += value; } } remove { lock (objectLock) { PreDrawEvent -= value; } } } // Explicit interface implementation required. // Associate IShape's event with // PostDrawEvent event EventHandler IShape.OnDraw { add { lock (objectLock) { PostDrawEvent += value; } } remove { lock (objectLock) { PostDrawEvent -= value; } } } // For the sake of simplicity this one method // implements both interfaces. public void 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()); } } } public class 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 public class 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."); } } public class Program { static void Main(string[] args) { Shape shape = new Shape(); Subscriber1 sub = new Subscriber1(shape); Subscriber2 sub2 = new Subscriber2(shape); shape.Draw(); // Keep the console window open in debug mode. System.Console.WriteLine("Press any key to exit."); System.Console.ReadKey(); } } }
/* C#接口事件示例Output: Sub1 receives the IDrawingObject event. Drawing a shape. Sub2 receives the IShape event. */
到此,關于“如何利用C#實現接口事件”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。