【问题标题】:C# EventHandler : How to handle EventHandler in client application?C# EventHandler:如何在客户端应用程序中处理 EventHandler?
【发布时间】:2020-02-01 18:26:56
【问题描述】:

我编写了一个处理电话呼叫的类库(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');
   }
}

【问题讨论】:

  • 抱歉问题不清楚,你能解释一下
  • 我详细阐述了这个问题@Clint
  • 您的意思仍然很不清楚-第二部分只是伪代码也无济于事。 “客户端应用程序”究竟是什么意思 - 什么客户端? Test 代码在哪里?请提供很多更多关于您正在尝试做的事情的信息。
  • @JonSkeet 我添加了更多信息。
  • Winforms 应用程序与其他应用程序是相同的 c# 库。因此,您的库可以公开自己的事件,winform“客户端”可以以与您的库订阅第三方库事件相同的方式订阅这些事件。 How can I make my own event in C#?

标签: c# eventhandler


【解决方案1】:

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

由于您希望事件处理机制发生在 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();
        }

    }
}

输出

【讨论】:

    猜你喜欢
    • 2013-07-13
    • 1970-01-01
    • 2018-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多