繁体   English   中英

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

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

我编写了一个处理电话呼叫的类库(dll)。 此类库具有处理电话事件的委托,例如 OnCallReceived、OnHoldCall 等。

所以现在我想将这个类库添加到我的 Windows 窗体应用程序中,并且能够在我的 Windows 窗体应用程序中处理电话呼叫事件(OnCall、OnHolde 等)。 怎样才能做到这一点?

例如

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

// 我希望这个事件发生在客户端应用程序中

由于您希望事件处理机制发生在客户端应用程序中,我认为这是另一个包含Main Class ,因此我创建了一个小控制台来复制问题场景


也上传到小提琴

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

    }
}

输出

在此处输入图片说明

暂无
暂无

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

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