Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在designer中查看表单时会出现错误-如何避免这种情况?_C#_.net_Winforms_Windows Forms Designer - Fatal编程技术网

C# 在designer中查看表单时会出现错误-如何避免这种情况?

C# 在designer中查看表单时会出现错误-如何避免这种情况?,c#,.net,winforms,windows-forms-designer,C#,.net,Winforms,Windows Forms Designer,在设计器中加载某些表单时,它们会显示错误。这是因为在它们的构造函数中,它们使用从配置文件加载的系统设置。当表单加载到设计器中时,它会使用一些随机路径,配置文件不在其中也就不足为奇了 例如。 找不到配置文件C:\Documents and Settings\Rory\Local Settings\Application Data\Microsoft\VisualStudio\8.0\ProjectAssemblys\it3dtcgg01\PrioryShared.dll.config 是否有某种方

在设计器中加载某些表单时,它们会显示错误。这是因为在它们的构造函数中,它们使用从配置文件加载的系统设置。当表单加载到设计器中时,它会使用一些随机路径,配置文件不在其中也就不足为奇了

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

是否有某种方法可以处理此问题,以便表单在设计器中正确显示? 例如:

更新: 好的,我仍然得到这个错误。构图是这样的

  • MyForm.cs

    • MyCustomControl.cs
在MyCustomControl的构造函数中,我将

if (!this.DesignMode)
{
    // Get settings from config file   <- this is where the error occurs
}
if(!this.DesignMode)
{
//从配置文件获取设置

这可以在所有页面和用户控件中使用。

只需将该逻辑放在
OnLoad
中,然后选中
DesignMode

protected override void OnLoad(System.EventArgs e)
{
    base.OnLoad(e);
    if (!this.DesignMode)
    {
        /* do stuff */
    }        
}

您应该在
this.DesignMode
上的复选框中包装项目,该复选框在设计器中设置为
true
,或者将项目移动到
OnLoad
或在设计器使用过程中不会调用的其他位置。

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

public bool IsDesignMode
{
   get
      {
         return (System.Diagnostics.Process.GetCurrentProcess().ProcessName == "devenv");
      }
}

如果设计器当前正在使用控件,则当我必须进行一些特殊处理时,我会这样做


public MyControl() {
  InitializeComponent();
  if (LicenseManager.UsageMode != LicenseUsageMode.Designtime)  {
    // Put stuff here that should not be run while in the designer
  }
}

我总是发现控件使用配置文件是一个错误。相反,控件应该具有可设置的属性,如果使用它的窗体希望从配置中设置这些属性,那么欢迎这样做。

Page?标记表明这是WinForm我认为Page是WPF或Silverlight概念。您误解了,w当我说“Page”时,我指的是System.Web.UI.Page,它源自控件。(!this.DesignMode)中的代码行仍在设计模式下执行-这怎么可能?注意:这里的关键点是将代码放入OnLoad而不是构造函数中。
public bool IsDesignMode
{
   get
      {
         return (System.Diagnostics.Process.GetCurrentProcess().ProcessName == "devenv");
      }
}

public MyControl() {
  InitializeComponent();
  if (LicenseManager.UsageMode != LicenseUsageMode.Designtime)  {
    // Put stuff here that should not be run while in the designer
  }
}