1. 程式人生 > 程式設計 >c#如何實現介面事件

c#如何實現介面事件

介面可以宣告事件。 下面的示例演示如何在類中實現介面事件。 這些規則基本上都與實現任何介面方法或屬性時的相同。

在類中實現介面事件

在類中宣告事件,然後在相應區域中呼叫它。

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); 
    } 
  } 

}

示例

下面的示例演示如何處理不太常見的情況:類繼承自兩個或多個介面,且每個介面都具有相同名稱的事件。 在這種情況下,你必須為至少其中一個事件提供顯式介面實現。 為事件編寫顯式介面實現時,還必須編寫 addremove 事件訪問器。 通常這些訪問器由編譯器提供,但在這種情況下編譯器不提供它們。

通過提供自己的訪問器,可以指定兩個事件是由類中的同一個事件表示,還是由不同事件表示。 例如,如果根據介面規範應在不同時間引發事件,可以在類中將每個事件與單獨實現關聯。 在下面的示例中,訂閱伺服器確定它們通過將形狀引用轉換為 IShape IDrawingObject 接收哪個 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.
*/

以上就是c#如何實現介面事件的詳細內容,更多關於c# 實現介面事件的資料請關注我們其它相關文章!