博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
How to: Implement Interface Events (C# Programming Guide)
阅读量:6842 次
发布时间:2019-06-26

本文共 5969 字,大约阅读时间需要 19 分钟。

Visual Studio 2010
 
 
 
 
2 out of 2 rated this helpful 
 

 

An  can declare an . The following example shows how to implement interface events in a class. Basically the rules are the same as when you implement any interface method or property.

To implement interface events in a class

  • Declare the event in your class and then invoke it in the appropriate areas.

    C#
     
    namespace ImplementInterfaceEvents{    public interface IDrawingObject    {        event EventHandler ShapeChanged;    }    public class MyEventArgs : EventArgs     {        // class members    }    public class Shape : IDrawingObject    {        public event EventHandler ShapeChanged;        public 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)        {            if(ShapeChanged != null)            {               ShapeChanged(this, e);            }        }    }}

The following example shows how to handle the less-common situation in which your class inherits from two or more interfaces and each interface has an event with the same name. In this situation, you must provide an explicit interface implementation for at least one of the events. When you write an explicit interface implementation for an event, you must also write the add and remove event accessors. Normally these are provided by the compiler, but in this case the compiler cannot provide them.

By providing your own accessors, you can specify whether the two events are represented by the same event in your class, or by different events. For example, if the events should be raised at different times according to the interface specifications, you can associate each event with a separate implementation in your class. In the following example, subscribers determine which OnDraw event they will receive by casting the shape reference to either anIShape or an IDrawingObject.

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();        }    }}/* Output:    Sub1 receives the IDrawingObject event.    Drawing a shape.    Sub2 receives the IShape event.*/

Tasks

Reference

Concepts

Did you find this helpful? Yes No
 
Community Content 
 
 
zLeos
event EventHandler ShapeChanged; I think you have to use generic EventHandler in the case of custom event args: event EventHandler
ShapeChanged;
  • 2/5/2012
 
Typo in the first code snipped
The "void ChangeShape()" method in the top code snipped should be made public.
Edit by SJ at MSFT: Thanks, I'll get that fixed.

转载地址:http://fczul.baihongyu.com/

你可能感兴趣的文章
Kafka管理工具介绍【转】
查看>>
OutOfMemoryError 到底能不能被捕获?
查看>>
EMMC 介绍【转】
查看>>
读jQuery之八(包装事件对象)
查看>>
Java SizeOf
查看>>
Java与XML(一):采用DOM操作XML文件
查看>>
[review]Design Pattern:Prototype
查看>>
第一节 9布尔运算符
查看>>
Java中serialVersionUID的解释
查看>>
小小问题
查看>>
Nagios记录系统监控日志
查看>>
每日英语:In the Future, Who Will Need Teachers?
查看>>
Android UI学习 - TableLayout
查看>>
Revit平面视图控制
查看>>
mysql 开启 登陆 停止
查看>>
java学习笔记------ PrintStream
查看>>
5、条件、循环和其他语句
查看>>
asp 文件上传(ASPUpload组件上传)
查看>>
MVC - 18.缓存
查看>>
Loadrunner11之禁用/启用Action
查看>>