.net 如何将勾号事件添加到非托管类中的System::windows::Forms::Timer
我试着像我在某个例子中发现的那样:.net 如何将勾号事件添加到非托管类中的System::windows::Forms::Timer,.net,visual-c++,event-handling,timer,c++-cli,.net,Visual C++,Event Handling,Timer,C++ Cli,我试着像我在某个例子中发现的那样: TimerID = gcnew System::Windows::Forms::Timer(); TimerID->Tick += gcnew System::EventHandler(this, &Bridge::timer1_Tick); 及 但由于以下错误,无法创建事件处理程序: 错误C3364:“System::EventHandler”:委托的参数无效 建造师;委托目标必须是指向成员的指针 作用 桥是非托管类。 所以我用这种方式宣布
TimerID = gcnew System::Windows::Forms::Timer();
TimerID->Tick += gcnew System::EventHandler(this, &Bridge::timer1_Tick);
及
但由于以下错误,无法创建事件处理程序:
错误C3364:“System::EventHandler”:委托的参数无效
建造师;委托目标必须是指向成员的指针
作用
桥是非托管类。
所以我用这种方式宣布TimerID:
gcroot<System::Windows::Forms::Timer ^> TimerID;
gcroot-TimerID;
我做错了什么?如何正确添加勾号事件?您没有发布足够的代码来诊断错误。这包括:
public ref class Bridge : public System::Windows::Forms::Form
{
Timer^ TimerID;
public:
Bridge(void) {
InitializeComponent();
TimerID = gcnew System::Windows::Forms::Timer();
TimerID->Tick += gcnew System::EventHandler(this, &Bridge::timer1_Tick);
}
private:
void Bridge::timer1_Tick(System::Object^ sender, System::EventArgs^ e) {
}
void InitializeComponent(void) {
// etc...
}
};
应该是这样的,Bridge是非托管类,我将gcroot用于TimerWell,这就解释了这一点。如何编译一个接受托管对象的本机方法有点神秘。您需要使用Marshal::GetDelegateForFunctionPointer()获取以本机函数为目标的委托对象。目标函数必须是静态的,不能是类的实例方法。或者只是将计时器设置为调用一个托管方法,该方法依次调用本机方法。您的意思是我的System::Void Bridge::timer1_Tick(System::Object^sender,System::EventArgs^e)也是非托管的,无法调用?我必须让它成为根吗?你为什么要这样做?如果您使用的是托管计时器,请使用托管类型。
public ref class Bridge : public System::Windows::Forms::Form
{
Timer^ TimerID;
public:
Bridge(void) {
InitializeComponent();
TimerID = gcnew System::Windows::Forms::Timer();
TimerID->Tick += gcnew System::EventHandler(this, &Bridge::timer1_Tick);
}
private:
void Bridge::timer1_Tick(System::Object^ sender, System::EventArgs^ e) {
}
void InitializeComponent(void) {
// etc...
}
};