Arabirim olaylarını uygulama (C# Programlama Kılavuzu)

Arabirim bir olay bildirebilir. Aşağıdaki örnekte, bir sınıfta arabirim olaylarının nasıl uygulandığı gösterilmektedir. Temel olarak kurallar, herhangi bir arabirim yöntemi veya özelliği uyguladığınızda olduğu gibi aynıdır.

Bir sınıfta arabirim olayları uygulamak için

sınıfınızda olayı bildirin ve uygun alanlarda çağırın.

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

}  

Örnek

Aşağıdaki örnek, sınıfınızın iki veya daha fazla arabirimden devraldığı ve her arabirimin aynı ada sahip bir olaya sahip olduğu daha az yaygın durumun nasıl işleneceğini gösterir. Bu durumda, olaylardan en az biri için açık bir arabirim uygulaması sağlamanız gerekir. Bir olay için açık arabirim uygulaması yazarken ve remove olay erişimcilerini de yazmanız add gerekir. Normalde bunlar derleyici tarafından sağlanır, ancak bu durumda derleyici bunları sağlayamaz.

Kendi erişimcilerinizi sağlayarak, iki olayın sınıfınızdaki aynı olayla mı yoksa farklı olaylarla mı temsil edileceğini belirtebilirsiniz. Örneğin, olayların arabirim belirtimlerine göre farklı zamanlarda tetiklenmesi gerekiyorsa, her olayı sınıfınızdaki ayrı bir uygulamayla ilişkilendirebilirsiniz. Aşağıdaki örnekte aboneler, şekil başvurularını veya IShapeIDrawingObjectöğesine dönüştürerek hangi OnDraw olayı alacaklarını belirler.

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.
*/

Ayrıca bkz.