温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

c#接口事件如何实现

发布时间:2020-10-28 16:10:22 来源:亿速云 阅读:218 作者:Leah 栏目:开发技术

这篇文章将为大家详细讲解有关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#接口事件如何实现就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI