C# EventHandler:如何在客户端应用程序中处理 EventHandler?

C# EventHandler : How to handle EventHandler in client application?

我写了一个 class 库 (dll) 来处理电话呼叫。此 class 库具有处理 phone 调用事件(例如 OnCallReceived、OnHoldCall 等)的委托。

所以现在我想将这个 class 库添加到我的 Windows Forms 应用程序中,并能够在我的 [=] 中处理 Phone 呼叫事件(OnCall、OnHolde 等) 17=] 表格申请。如何实现?

例如

//My Class Library

Class Test
{
   ThirdParyLibrary tpl

   public Test()
   {
      tpl= new tpl();
      tpl.OnReceiveCall += Handler(OnReceiveCall);     
   }

   public void OnReceiveCall()
   {
      //i want this event to take place in client app
   }  
}

//My Windows Forms App

Client App

public main()
{
   Test t =new Test()
   //i want OnReceiveCall to be processed here
   //t.OnReceiveCall
   {
      Message.Show('You received a call');
   }
}

// i want this event to take place in client app

由于您希望事件处理机制发生在 Client 应用程序中,我想这是另一个包含 MainClass,我创建了复制问题场景的小型控制台


Uploaded to fiddle as well

using System;

namespace Test
{
    public class ThirdPartyLibrary
    {
        public delegate void dEeventRaiser();
        public event dEeventRaiser OnReceiveCall;
        public string IncomingCall(int x)
        {

            if (x > 0 && OnReceiveCall != null)
            { OnReceiveCall(); return "Valid "+x.ToString(); }
            return "Invalid"+x.ToString();
        }
    }



    public class EventSubscription
    {

        public EventSubscription()
        {
            ThirdPartyLibrary a = new ThirdPartyLibrary();
            a.OnReceiveCall += HandleTheCall;
            var iAnswer = a.IncomingCall(24198724);
            Console.WriteLine("Call received from "+iAnswer);
        }

        public virtual void HandleTheCall()
        {
            Console.WriteLine("Default way I handle the call");
        }

    }

    public class Program : EventSubscription
    {
        public override void HandleTheCall()
        {
            Console.WriteLine("Override sucessful, new way to handle the call ");
        }

       static void Main(string [] args)
        {

          Program pb = new Program();  // Control goes EnventSubscription constructor as it is derived 
            Console.Read();
        }

    }
}

输出: