【问题标题】:Visual Studio designer crashes when overrideing WndProc覆盖 WndProc 时 Visual Studio 设计器崩溃
【发布时间】:2014-10-22 14:51:24
【问题描述】:

我有一个继承自 System.Windows.Forms.Control 的控件,它重写了 WndProc 函数以将消息传递给外部 DLL。函数看起来像

public class GraphicsPanel : Control
{
        bool designMode;
        EngineWrapper Engine;

        public GraphicsPanel()
        {
            designMode = (LicenseManager.UsageMode == LicenseUsageMode.Designtime);
            this.SetStyle(ControlStyles.Selectable, true);
            this.TabStop = true;
        }

        public void SetEngine(EngineWrapper Engine)
        {
            this.Engine = Engine;
        }
    
        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);

            if(designMode) // Tried this option
                return;

            if (!designMode && Engine != null) // And this option
                Engine.ProcessWindowMessage(m.Msg, m.WParam, m.LParam);
        }
}

如果我在这个函数中提到了引擎,它会崩溃,因为在显示设计器时显然没有加载外部 DLL。我收到错误消息“无法加载文件或程序集“...”或其依赖项之一。

我当然应该补充一点,这段代码都是在运行时工作的,而不是在设计时工作的。每当我想使用设计器时,都必须注释掉两行引擎代码,这很烦人。

关于如何让设计器在包含这行代码时正确工作的任何建议。

【问题讨论】:

    标签: c# winforms


    【解决方案1】:

    您是否需要在设计时执行 WndProc? 如果没有,您可以尝试在 if 中添加条件:

    bool designMode = (LicenseManager.UsageMode == LicenseUsageMode.Designtime);
    if (!designMode && Engine != null)
        ...
    

    您需要导入 System.ComponentModel 命名空间。

    Detecting design mode from a Control's constructor

    【讨论】:

    • 是否有编译器指令可以做同样的事情?这当然是一个好消息,但它并没有解决设计者的错误。
    • 我不这么认为,请查看this。您是说designMode 在设计时是错误的吗?您能否提供有关该错误的更多信息?
    • designMode 是正确的,但由于变量 Engine 引用了另一个未加载的 dll,因此无法为设计器编译。
    • Engine 是如何定义/创建的?
    • 它是对dll中定义的对象的引用。它是在运行时创建的,因此是空检查。
    【解决方案2】:

    我通过将引擎引用移动到不同的函数来解决这个问题。这很奇怪,但确实有效。

        void PassMessage(Message m)
        {
            if (Engine != null)
                Engine.ProcessWindowMessage(m.Msg, m.WParam, m.LParam);
        }
    
        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);
    
            if (!designMode)
                PassMessage(m);
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多