日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
C#接口事件的實現(xiàn)解析

C#接口事件的實現(xiàn)是如何的呢?下面的C#接口事件示例演示如何在類中實現(xiàn)接口事件。實現(xiàn)C#接口事件的規(guī)則與實現(xiàn)任何接口方法或?qū)傩缘囊?guī)則基本相同。

我們提供的服務有:成都網(wǎng)站制作、成都網(wǎng)站設(shè)計、外貿(mào)營銷網(wǎng)站建設(shè)、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認證、雨城ssl等。為上1000+企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務,是有科學管理、有技術(shù)的雨城網(wǎng)站制作公司

C#接口事件實例:

在類中實現(xiàn)接口事件,在類中聲明事件,然后在適當?shù)膮^(qū)域調(diào)用該事件。

 
 
 
  1. public interface IDrawingObject  
  2. {  
  3. event EventHandler ShapeChanged;  
  4. }  
  5. public class MyEventArgs : EventArgs {…}  
  6. public class Shape : IDrawingObject  
  7. {  
  8. event EventHandler ShapeChanged;  
  9. void ChangeShape()  
  10. {  
  11. // Do something before the event…  
  12. OnShapeChanged(new MyEventsArgs(…));  
  13. // or do something after the event.   
  14. }  
  15. protected virtual void OnShapeChanged(MyEventArgs e)  
  16. {  
  17. if(ShapeChanged != null)  
  18. {  
  19.    ShapeChanged(this, e);  
  20. }  
  21. }  

C#接口事件示例

下面的示例演示如何處理以下的不常見情況:您的類是從兩個以上的接口繼承的,每個接口都含有同名事件)。在這種情況下,您至少要為其中一個事件提供顯式接口實現(xiàn)。為事件編寫顯式接口實現(xiàn)時,必須編寫 add 和 remove 事件訪問器。這兩個事件訪問器通常由編譯器提供,但在這種情況下編譯器不能提供。

您可以提供自己的訪問器,以便指定這兩個事件是由您的類中的同一事件表示,還是由不同事件表示。例如,根據(jù)接口規(guī)范,如果事件應在不同時間引發(fā),則可以將每個事件與類中的一個單獨實現(xiàn)關(guān)聯(lián)。在下面的示例中,訂戶將形狀引用強制轉(zhuǎn)換為 IShape 或 IDrawingObject,從而確定自己將會接收哪個 OnDraw 事件。

C#接口事件代碼:

 
 
 
  1. namespace WrapTwoInterfaceEvents  
  2. {  
  3. using System;  
  4.  
  5. public interface IDrawingObject  
  6. {  
  7. // Raise this event before drawing  
  8. // the object.  
  9. event EventHandler OnDraw;  
  10. }  
  11. public interface IShape  
  12. {  
  13. // Raise this event after drawing  
  14. // the shape.  
  15. event EventHandler OnDraw;  
  16. }  
  17.  
  18.  
  19. // Base class event publisher inherits two  
  20. // interfaces, each with an OnDraw event  
  21. public class Shape : IDrawingObject, IShape  
  22. {  
  23. // Create an event for each interface event  
  24. event EventHandler PreDrawEvent;  
  25. event EventHandler PostDrawEvent;  
  26.  
  27. object objectLock = new Object();  
  28.  
  29. // Explicit interface implementation required.  
  30. // Associate IDrawingObject's event with  
  31. // PreDrawEvent  
  32. event EventHandler IDrawingObject.OnDraw  
  33. {  
  34. add  
  35. {  
  36. lock (objectLock)  
  37. {  
  38. PreDrawEvent += value;  
  39. }  
  40. }  
  41. remove  
  42. {  
  43. lock (objectLock)  
  44. {  
  45. PreDrawEvent -= value;  
  46. }  
  47. }  
  48. }  
  49. // Explicit interface implementation required.  
  50. // Associate IShape's event with  
  51. // PostDrawEvent  
  52. event EventHandler IShape.OnDraw  
  53. {  
  54. add   
  55. {  
  56. lock (objectLock)  
  57. {  
  58. PostDrawEvent += value;  
  59. }  
  60. }  
  61. remove  
  62. {  
  63. lock (objectLock)  
  64. {  
  65. PostDrawEvent -= value;  
  66. }  
  67. }  
  68.  
  69.  
  70. }  
  71.  
  72. // For the sake of simplicity this one method  
  73. // implements both interfaces.   
  74. public void Draw()  
  75. {  
  76. // Raise IDrawingObject's event before the object is drawn.  
  77. EventHandler handler = PreDrawEvent;  
  78. if (handler != null)  
  79. {  
  80. handler(this, new EventArgs());  
  81. }  
  82. Console.WriteLine("Drawing a shape.");  
  83.  
  84. // RaiseIShape's event after the object is drawn.  
  85. handler = PostDrawEvent;  
  86. if (handler != null)  
  87. {  
  88. handler(this, new EventArgs());  
  89. }  
  90. }  
  91. }  
  92. public class Subscriber1  
  93. {  
  94. // References the shape object as an IDrawingObject  
  95. public Subscriber1(Shape shape)  
  96. {  
  97. IDrawingObject d = (IDrawingObject)shape;  
  98. d.OnDraw += new EventHandler(d_OnDraw);  
  99. }  
  100.  
  101. void d_OnDraw(object sender, EventArgs e)  
  102. {  
  103. Console.WriteLine("Sub1 receives the IDrawingObject event.");  
  104. }  
  105. }  
  106. // References the shape object as an IShape  
  107. public class Subscriber2  
  108. {  
  109. public Subscriber2(Shape shape)  
  110. {  
  111. IShape d = (IShape)shape;  
  112. d.OnDraw += new EventHandler(d_OnDraw);  
  113. }  
  114.  
  115. void d_OnDraw(object sender, EventArgs e)  
  116. {  
  117. Console.WriteLine("Sub2 receives the IShape event.");  
  118. }  
  119. }  
  120.  
  121.  
  122. public class Program  
  123. {  
  124. static void Main(string[] args)  
  125. {  
  126. Shape shape = new Shape();  
  127. Subscriber1 sub = new Subscriber1(shape);  
  128. Subscriber2 sub2 = new Subscriber2(shape);  
  129. shape.Draw();  
  130.  
  131. // Keep the console window open in debug mode.  
  132. System.Console.WriteLine("Press any key to exit.");  
  133. System.Console.ReadKey();  
  134. }  
  135. }  
  136.  
 
 
 
  1. /* C#接口事件示例Output:  
  2. Sub1 receives the IDrawingObject event.  
  3. Drawing a shape.  
  4. Sub2 receives the IShape event.  
  5. */ 

C#接口事件的實現(xiàn)以及使用的一些內(nèi)容就向你介紹到這里,希望對你了解和學習C#接口事件有所幫助。


當前題目:C#接口事件的實現(xiàn)解析
新聞來源:http://www.5511xx.com/article/djpgjho.html