使用c++/cli并在c#中使用它的事件处理

event handling using c++/cli and using it in c#

本文关键字:事件处理 c++ cli 并在 使用      更新时间:2023-10-16

-每当调用方法showmessage时,我都想引发一个事件。我想在C#密码

-我已经为它写了事件。

-我在Initialize函数中所做的将委托与功能显示消息

-如何在c#中使用此事件

C++/CLI
delegate void progressmsgdisplay(System::String ^ message);
progressmsgdisplay  ^ progressMsgNotify;
void Mclass::ShowMessage(System::String ^ message)
{ 
MessageBox(NULL, m_msg, m_box, NULL);
notify(message);
}
event progressmsgdisplay ^ notify 
{
    void add(progressmsgdisplay ^ d) 
    {
        progressMsgNotify += d;
    }

    void remove(progressmsgdisplay ^ d) 
    {
        progressMsgNotify -= d;
    }

    void raise(System::String ^ msg)
    {
       progressmsgdisplay ^ tmp = progressMsgNotify;
               if (tmp) 
        {
        tmp->Invoke(msg);
        }
    }
}
//void Mclass::Initialize(System::String ^ strProgressMsg)
//{
// progressMsgNotify=gcnew progressmsgdisplay(this,&Mclass::ShowMessage);
//}

-Mclass是在其中声明和定义上述所有的类的名称

C#
void display(string progressnotification)
 {
    Console.Out.WriteLine(progressnotification);
 }
void initialize()
{
 first = new Mclass();
 first.notify()+=display;
}

这就成功了

为什么不能在c++/cli中使用EventHandler类并在c#中订阅它

//C++/CLI
public ref class SomeClass
{
    public:
    event EventHandler^ someEvent;
}
//C#
class Program
{
    static void Main(string[] args)
    {
        SomeClass testclass = new SomeClass();
        testclass.someEvent += someEventHandler;
    }
    private void someEventHandler(Object obj, EventArgs args)
    {
    }
}

我还没试过这个。但我想值得一试。