我用 C++/CLI 写了一个类,它继承自System.Windows.Forms.UserControl
. 它工作正常,但每当我尝试在设计器视图中查看包含此控件的某个表单时,它会因以下错误而崩溃:
如果我单击“转到代码”,我会被发送到MainForm.Designer.cs
,在mConsole
我的控件实例添加到表单的行中。有人告诉我它没有声明,但在下面几行就完成了。BeginInit
是 new'ing 之后调用的第一个函数mConsole
,因此Init
在设置所有属性之前调用。一切看起来都在这一端。
控件的 dll 是使用/clr
开关构建的,使用它的 Windows 窗体项目具有引用集。它每次都能完美地构建和运行。唯一的问题是设计视图。每次我需要添加一些组件时,我都必须手动编辑设计器文件,这真的很麻烦。
这是我的一些控件的代码:
//Console.h
namespace MConsole {
public ref class MCConsole : public System::Windows::Forms::UserControl,
public System::ComponentModel::ISupportInitialize {
public:
MCConsole();
virtual ~MCConsole();
!MCConsole();
//Skipping custom properties and methods...
virtual void BeginInit();
virtual void EndInit();
protected:
virtual void OnPaint( System::Windows::Forms::PaintEventArgs ^ pe ) override;
};
//Console.cpp
#include "Console.h"
void MCConsole::Init(){
if( m_bInitialized )
return;
// Initialize native instance
m_pNativeInstance->Init( (HWND)this->Handle.ToPointer() );
m_bInitialized = true;
// Initialize timer
m_TimerTickHandler = gcnew System::EventHandler( this, &MCConsole::Render );
m_Timer.Tick += m_TimerTickHandler;
m_Timer.Interval = Math::Round<float, int>(1000.0f/FRAMES_PER_SECOND);
m_Timer.Start();
// Initialize event handlers
m_MouseWheelHandler = gcnew System::Windows::Forms::MouseEventHandler( this, &MCConsole::MouseWheelHandlerZoom );
this->MouseWheel += m_MouseWheelHandler;
m_ResizeHandler = gcnew System::EventHandler( this, &MCConsole::Resize );
this->SizeChanged += m_ResizeHandler;
m_MouseDownHandler = gcnew System::Windows::Forms::MouseEventHandler( this, &MCConsole::MouseDownHandler );
this->MouseDown += m_MouseDownHandler;
m_MouseLeaveHandler = gcnew System::EventHandler( this, &MCConsole::MouseLeaveHandler );
this->MouseLeave += m_MouseLeaveHandler;
m_MouseMoveHandler = gcnew System::Windows::Forms::MouseEventHandler( this, &MCConsole::MouseMoveHandler );
this->MouseMove += m_MouseMoveHandler;
m_MouseUpHandler = gcnew System::Windows::Forms::MouseEventHandler( this, &MCConsole::MouseUpHandler );
this->MouseUp += m_MouseUpHandler;
m_KeyPressHandler = gcnew System::Windows::Forms::KeyPressEventHandler( this, &MCConsole::KeyPressHandler );
this->KeyPress += m_KeyPressHandler;
m_DropHandler = gcnew System::Windows::Forms::DragEventHandler( this, &MCConsole::DropHandler );
this->DragDrop += m_DropHandler;
m_VisibleChangedHandler = gcnew System::EventHandler( this, &MCConsole::VisibleChangedHandler );
this->VisibleChanged += m_VisibleChangedHandler;
// Initialize layout wrapper
m_hRegionLayout = gcnew MCLayout( *m_pNativeInstance->getRegionLayout() );
m_bInitialized = true;
}
void MCConsole::BeginInit(){
Init();
}
void MCConsole::EndInit(){}
void MCConsole::OnPaint( System::Windows::Forms::PaintEventArgs ^ pe ) {
System::Windows::Forms::UserControl::OnPaint( pe );
if( m_bInitialized )
m_pNativeInstance->Refresh();
}
我错过了什么或搞砸了什么?或者如何调试 Designer 崩溃?
更新:我刚刚尝试启动另一个 Visual Studio 实例并附加到另一个 VS 的进程。我将前者配置为中断各种异常,我注意到当我打开设计视图时,我得到'System.MissingMethodException' 和 Additional information: Attempted to access a missing member。最终导致(点击继续)屏幕截图中的消息。如果我知道缺少哪种方法就好了……