简体   繁体   English

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

[英]C# EventHandler : How to handle EventHandler in client application?

I have written a class library(dll) which handle Telephony calls.我编写了一个处理电话呼叫的类库(dll)。 This class library has delegates that handle phone call events such as OnCallReceived, OnHoldCall etc.此类库具有处理电话事件的委托,例如 OnCallReceived、OnHoldCall 等。

So now i want to add this class library to my Windows Forms App, and be able to handle Phone Call events(OnCall, OnHolde etc) in my Windows Forms application.所以现在我想将这个类库添加到我的 Windows 窗体应用程序中,并且能够在我的 Windows 窗体应用程序中处理电话呼叫事件(OnCall、OnHolde 等)。 How can achieve this?怎样才能做到这一点?

For example例如

//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 // 我希望这个事件发生在客户端应用程序中

Since you want the Event Handling mechanism to take place in the Client App, which I suppose is another Class containing Main , I have created a small console that replicates the problem scenario由于您希望事件处理机制发生在客户端应用程序中,我认为这是另一个包含Main Class ,因此我创建了一个小控制台来复制问题场景


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

    }
}

Output :输出

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM