无法将Windows窗体设计器与使用C ++ / CLI编写的用户控件一起使用

时间:2012-09-06 14:42:46

标签: .net winforms c++-cli windows-forms-designer

我在C ++ / CLI中编写了一个继承自System.Windows.Forms.UserControl的类。它工作正常,但每当我尝试在设计器视图中查看包含此控件的某个表单时,它会因此错误而崩溃:

Designer error

如果我点击“转到代码”,我会收到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崩溃?

UPDATE :我刚刚尝试启动另一个Visual Studio实例并附加到另一个VS的进程。我配置前者以打破各种异常,我注意到当我打开设计视图时,我得到'System.MissingMethodException'及其他信息:尝试访问缺少的成员。最终导致(点击继续)到屏幕截图中的消息。如果我只知道缺少哪种方法......

1 个答案:

答案 0 :(得分:3)

它正在查找DLL的旧副本。也许工具箱文件夹有一个旧的副本,将其从工具箱中删除并重新添加。也许GAC有一个旧版本,如果它在运行时工作正常,则不太可能。

如果找不到,请从Visual Studio命令提示符运行Fuslogvw.exe。记录所有绑定,以便您可以看到CLR从中检索程序集的位置。