2

当我在设计器中加载某些表单时,它们会显示错误。这是因为在它们的构造函数中,它们使用从配置文件加载的系统设置。当表单在设计器中加载时,它使用一些随机路径,不出所料,配置文件不存在。

例如。找不到配置文件 C:\Documents and Settings\Rory\Local Settings\Application Data\Microsoft\VisualStudio\8.0\ProjectAssemblies\it3dtcgg01\PrioryShared.dll.config。

有什么方法可以解决这个问题,以便表单在设计器中正确显示?例如:

if (!inDesignerMode) 
{ 
    loadSettingsFromConfigFile();
}

更新:好的,我仍然收到此错误。组成是这样的

  • MyForm.cs

    • 我的自定义控件.cs

在 MyCustomControl 的构造函数中,我放了

if (!this.DesignMode)
{
    // Get settings from config file   <- this is where the error occurs
}

但正是在那条线上,我仍然在设计器中遇到错误。是什么赋予了?

更新:值得注意的是这个描述如何调试设计时控件的链接。

更新:在对象的构造函数 ( MSDN ) 中调用时,Control.DesignMode 未设置为 true !所以这种代码应该放在 onLoad 中。或者,您可以使用这样的方法

4

6 回答 6

4

不如简单地把这个逻辑放进去OnLoad,然后检查一下DesignMode

protected override void OnLoad(System.EventArgs e)
{
    base.OnLoad(e);
    if (!this.DesignMode)
    {
        /* do stuff */
    }        
}
于 2009-02-05T22:49:02.793 回答
2

您应该将项目包装在 check on 中this.DesignMode,该选项设置为true在设计器中时设置,或者将它们移动到OnLoad或在设计器使用期间不会被调用的其他地方。

于 2009-02-05T22:49:19.877 回答
1

或者,您可以执行以下操作:

public bool IsDesignMode
{
   get
      {
         return (System.Diagnostics.Process.GetCurrentProcess().ProcessName == "devenv");
      }
}
于 2009-02-05T22:51:53.023 回答
0
Page.DesignMode

这可用于所有页面和用户控件。

于 2009-02-05T22:48:54.650 回答
0

如果控件当前由设计器使用,当我必须进行一些特殊处理时,这就是我所做的。


public MyControl() {
  InitializeComponent();
  if (LicenseManager.UsageMode != LicenseUsageMode.Designtime)  {
    // Put stuff here that should not be run while in the designer
  }
}
于 2009-04-09T10:46:47.227 回答
0

我一直认为控件使用配置文件是错误的。相反,控件应该具有可设置的属性,如果使用它的表单想要从配置中设置这些属性,那么欢迎这样做。

于 2009-04-10T02:50:40.877 回答