您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)c#接口事件如何實(shí)現(xiàn),文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。
接口可以聲明事件。 下面的示例演示如何在類中實(shí)現(xiàn)接口事件。 這些規(guī)則基本上都與實(shí)現(xiàn)任何接口方法或?qū)傩詴r(shí)的相同。
在類中實(shí)現(xiàn)接口事件
在類中聲明事件,然后在相應(yīng)區(qū)域中調(diào)用它。
namespace ImplementInterfaceEvents { public interface IDrawingObject { event EventHandler ShapeChanged; } public class MyEventArgs : EventArgs { // class members } public class Shape : IDrawingObject { public event EventHandler ShapeChanged; void ChangeShape() { // Do something here before the event… OnShapeChanged(new MyEventArgs(/*arguments*/)); // or do something here after the event. } protected virtual void OnShapeChanged(MyEventArgs e) { ShapeChanged?.Invoke(this, e); } } }
示例
下面的示例演示如何處理不太常見(jiàn)的情況:類繼承自兩個(gè)或多個(gè)接口,且每個(gè)接口都具有相同名稱的事件。 在這種情況下,你必須為至少其中一個(gè)事件提供顯式接口實(shí)現(xiàn)。 為事件編寫(xiě)顯式接口實(shí)現(xiàn)時(shí),還必須編寫(xiě) add
和 remove
事件訪問(wèn)器。 通常這些訪問(wèn)器由編譯器提供,但在這種情況下編譯器不提供它們。
通過(guò)提供自己的訪問(wèn)器,可以指定兩個(gè)事件是由類中的同一個(gè)事件表示,還是由不同事件表示。 例如,如果根據(jù)接口規(guī)范應(yīng)在不同時(shí)間引發(fā)事件,可以在類中將每個(gè)事件與單獨(dú)實(shí)現(xiàn)關(guān)聯(lián)。 在下面的示例中,訂閱服務(wù)器確定它們通過(guò)將形狀引用轉(zhuǎn)換為 IShape
或 IDrawingObject
接收哪個(gè) OnDraw
事件。
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 #region IDrawingObjectOnDraw event EventHandler IDrawingObject.OnDraw { add { lock (objectLock) { PreDrawEvent += value; } } remove { lock (objectLock) { PreDrawEvent -= value; } } } #endregion // 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. PreDrawEvent?.Invoke(this, EventArgs.Empty); Console.WriteLine("Drawing a shape."); // Raise IShape's event after the object is drawn. PostDrawEvent?.Invoke(this, EventArgs.Empty); } } public class Subscriber1 { // References the shape object as an IDrawingObject public Subscriber1(Shape shape) { IDrawingObject d = (IDrawingObject)shape; d.OnDraw += 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 += 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(); } } } /* Output: Sub1 receives the IDrawingObject event. Drawing a shape. Sub2 receives the IShape event. */
關(guān)于c#接口事件如何實(shí)現(xiàn)就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。