实现接口事件

  • 在类中实现接口事件

      可以在接口中声明事件,然后在类中实现该事件的引用。在为方法订阅事件时,可以订阅接口声明的事件,也可以订阅类中的事件。

    class InterfaceEvent
    {
        
        static void Main(string[] args)
        {
            Shape s = new Shape();
            IDrawingObject ido = s;
            //为HandlerShapeChanged方法订阅接口中声明的事件
            ido.ShapeChanged += HandlerShapeChanged;

            //为HandlerShapeChanged方法订阅实现接口的事件
            s.ShapeChanged += HandlerShapeChanged;
            s.ChangeShape();

            Console.Read();
        }
        
        static void HandlerShapeChanged(object sender, CustomEventArgs e)
        {
            Console.WriteLine("Received event.Shape area is now {0}", e.Area);
        }
    }
    public class CustomEventArgs : EventArgs
    {
        private double area;
        public double Area
        {
            get { return area; }
            set { area = value; }
        }
        public CustomEventArgs(double a)
        {
            area = a;
        }
    }
    public interface IDrawingObject
    {
        event EventHandler<CustomEventArgs> ShapeChanged;
    }
    public class Shape : IDrawingObject
    {
        public event EventHandler<CustomEventArgs> ShapeChanged;
        public void ChangeShape()
        {
            OnShapeChanged(new CustomEventArgs(10));
        }
        protected virtual void OnShapeChanged(CustomEventArgs e)
        {
            EventHandler<CustomEventArgs> hander = ShapeChanged;
            if (hander != null)
            {
                hander(this, e);
            }
        }
    }

输出如下:

  • 事件访问器

    事件访问器类似属性访问器,事件访问器被命名为add和remove,一般情况下不需提供自定义事件访问器,因为在编译时会自动添加事件访问器。

    add事件访问器:当客户端订阅该事件,调用该访问器。

    remove事件访问器:当客户端取消订阅事件,调用该访问器。

class Events : IDrawingObject
{        
    event EventHandler PreDrawEvent;

    event EventHandler IDrawingObject.OnDraw
    {
        add
        {
            lock (PreDrawEvent)
            {
                PreDrawEvent += value;
            }
        }
        remove
        {
            lock (PreDrawEvent)
            {
                PreDrawEvent -= value;
            }
        }
    }

}
  •  类继承同名事件声明的多个接口

    当类继承多个接口,接口中有同名事件声明,则需要借助add和remove事件访问器显示接口实现。

    class TwoInterfaceEvent
    {
        static void Main(string[] args)
        {
            Shape shape = new Shape();
            Subscriber1 sub1 = new Subscriber1(shape);
            Subscriber2 sub2 = new Subscriber2(shape);
            shape.Draw();

            Console.Read();
        }
    }

    public interface IDrawingObject
    {
        event EventHandler OnDraw;
    }

    public interface IShape
    {
        event EventHandler OnDraw;
    }

    public class Shape : IDrawingObject, IShape
    {
        event EventHandler PreDrawEvent;
        event EventHandler PostDrawEvent;

        object objectLock = new object();

        /// <summary>
        /// 明确接口实现
        /// PreDrawEvent与IDrawingObject.OnDraw关联
        /// </summary>
        event EventHandler IDrawingObject.OnDraw
        {
            add
            {
                lock (objectLock)
                {
                    PreDrawEvent += value;
                }
            }
            remove
            {
                lock (objectLock)
                {
                    PreDrawEvent -= value;
                }
            }
        }

        /// <summary>
        /// 明确接口实现
        /// PostDrawEvent与IShape.OnDraw关联
        /// </summary>
        event EventHandler IShape.OnDraw
        {
            add
            {
                lock (objectLock)
                {
                    PostDrawEvent += value;
                }
            }
            remove
            {
                lock (objectLock)
                {
                    PostDrawEvent -= value;
                }
            }
        }

        /// <summary>
        /// 同时引用两个接口事件
        /// </summary>
        public void Draw()
        {
            //引用事件PreDrawEvent
            EventHandler handler = PreDrawEvent;
            if (handler != null)
            {
                handler(this, new EventArgs());
            }
            Console.WriteLine("Raised IDrawingObject's event.");

            //引用事件PostDrawEvent
            handler = PostDrawEvent;
            if (handler != null)
            {
                handler(this, new EventArgs());
            }
            Console.WriteLine("Raised IShape's event.");
        }

    }

    public class Subscriber1
    {
        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.");
        }
    }
    public class Subscriber2
    {
        public Subscriber2(Shape shape)
        {
            IShape s = (IShape)shape;
            s.OnDraw+=new EventHandler(s_OnDraw);
        }
        void s_OnDraw(object sender, EventArgs e)
        {
            Console.WriteLine("Sub2 receives the IShape event.");
        }
    }

输出如下:

posted @ 2012-06-25 19:45  蒹葭白露  阅读(2277)  评论(0编辑  收藏  举报