简体   繁体   English

从另一个应用程序订阅dll中的事件

[英]Subscribe to event in dll from another application

I've been looking all over for a possible solution, but none seem to come close to what I actually need. 我一直在寻找可能的解决方案,但似乎没有一个与我真正需要的解决方案接近。 I have a windows form which outputs to a class library (dll). 我有一个Windows窗体,可输出到类库(dll)。 I reference this dll into a console application and then launch the GUI in the dll. 我将此dll引用到控制台应用程序中,然后在dll中启动GUI。 I want to be able to subscribe to a control event and do what ever code in my console application. 我希望能够订阅一个控制事件并在控制台应用程序中执行任何代码。 I have no problem when I want to read or write properties in the dll directly from my console app. 当我想直接从控制台应用程序读取或写入dll中的属性时,我没有问题。 Example: 例:

MyDll.MyClass myObj = new MyDll.MyClass();
myObj.Textbox1.Text = "Hello World!";

However, I would like to subscribe to the TextChanged event in my dll and output the new text to my console app. 但是,我想在dll中订阅TextChanged事件,并将新文本输出到控制台应用程序。 Something along the lines of: 类似于以下内容:

public void textbox1_TextChaned(object sender, EventArgs e)
{
    Console.WriteLine(myObj.textbox1.Text);
}

Is there any way to subscribe directly to that event? 有什么办法可以直接订阅该事件? or some other event? 还是其他事件?

  1. Set the modifier of textbox1 to public textbox1的修饰符设置为public

  2. Subscribe to the TextChanged event: 订阅TextChanged事件:

    myObj.Textbox1.TextChanged += textbox1_TextChaned; myObj.Textbox1.TextChanged + = textbox1_TextChaned;

The following code in a console app works for me. 控制台应用程序中的以下代码对我有用。 I'm referencing a Windows Form Application rather than a DLL, but I don't think there should be much difference: 我指的是Windows窗体应用程序,而不是DLL,但我认为应该没有太大区别:

 class Program
  {
    static WindowsFormsApplication1.Form1 frm;
    static void Main(string[] args)
    {
      frm = new WindowsFormsApplication1.Form1();
      frm.textBox1.TextChanged += textBox1_TextChanged;
      System.Windows.Forms.Application.Run(frm);

    }
    static void textBox1_TextChanged(object sender, EventArgs e)
    {
      Console.WriteLine((frm.textBox1.Text));
    }
  }

This doesn't answer your question directly, but it will solve your problem in a neater way, IMO. 这不会直接回答您的问题,但是它将以更整洁的方式解决您的问题,IMO。

  1. In your DLL define an interface (IEventProcessor). 在您的DLL中定义一个接口(IEventProcessor)。
  2. In your console application implement the interface (ConsoleEventProcessor). 在您的控制台应用程序中,实现接口(ConsoleEventProcessor)。
  3. Pass an instance through the constructor of the form 通过表单的构造函数传递实例
  4. call its methods from the events in the form. 从表单中的事件调用其方法。

In the console app: 在控制台应用程序中:

IEventProcessor processor = new ConsoleEventProcessor();    
MyDll.MyClass myObj = new MyDll.MyClass(processor);

Then in the form: 然后以以下形式:

IEventProcessor _processor;
// constructor
public MyClass(IEventProcessor processor)
{
   _processor = processor;
}

public void textbox1_TextChaned(object sender, EventArgs e)
{ 
    // pass whatever parameters you need to use in the method
    _processor.ProcessText1Changed(textbox1.Text);
}

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

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