从另一个应用程序订阅dll中的事件 [英] Subscribe to event in dll from another application

查看:104
本文介绍了从另一个应用程序订阅dll中的事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找一种可能的解决方案,但似乎没有一个与我真正需要的解决方案接近.我有一个Windows窗体,它输出到类库(dll).我将此dll引用到控制台应用程序中,然后在dll中启动GUI.我希望能够订阅一个控制事件并在控制台应用程序中执行任何代码. 当我想直接从控制台应用程序读取或写入dll中的属性时,我没有问题.示例:

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). I reference this dll into a console application and then launch the GUI in the dll. 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. Example:

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

但是,我想在dll中订阅TextChanged事件,并将新文本输出到控制台应用程序.类似于以下内容:

However, I would like to subscribe to the TextChanged event in my dll and output the new text to my console app. 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. textbox1的修饰符设置为public

订阅TextChanged事件:

myObj.Textbox1.TextChanged + = textbox1_TextChaned;

myObj.Textbox1.TextChanged += textbox1_TextChaned;

控制台应用程序中的以下代码对我有用.我指的是Windows窗体应用程序,而不是DLL,但我认为应该没有太大区别:

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:

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

这篇关于从另一个应用程序订阅dll中的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆