如何在构造函数中包含 Visual Studio 在设计时不会执行的代码?

发布于 2024-08-12 11:40:23 字数 240 浏览 5 评论 0原文

我的用户控件的构造函数中有一个方法调用,该方法在设计时无法正常工作(连接到数据库),当我尝试将该控件添加到 GUI 设计器时,Visual Studio 就退出了。
当然,我可以将该代码分解为一个单独的方法,但我不喜欢这样的想法:每次使用该对象时,我都需要记住执行对该对象的功能至关重要的某个方法(这就是构造函数)为了!)。

是否有类似预处理器符号之类的东西可以用来标记我的代码,以便 Visual Studio 不会在设计时尝试执行该代码?

I have a method call in the constructor of my user control that does something that won't work at design time (connecting to a database), and Visual Studio just bailed out when I tried to add that control to the GUI designer.
Sure, I can factor out that code to a separate method, but I don't like the idea that every time I use that object I need to remember to execute a certain method which is essential to that object's function (that's what the constructor is for!).

Is there something like a preprocessor symbol that I can mark my code with so that Visual Studio won't try to execute that code at design time?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

看透却不说透 2024-08-19 11:40:24

正如其他人所说,您可以使用 DesignMode Component 类的 属性。但是,您将无法在控件的构造函数中执行此操作。在构造函数和构造函数调用的方法中,DesignMode 属性始终为 false。要解决此问题,请重构代码以在 OnLoad() 回调中连接到数据库。 DesignMode 属性此时有效。请参阅此处了解原因(查找文章的 DesignMode 部分) 。

我刚刚遇到这个博客条目,它描述了如何使用System.ComponentModel.LicenseManager.UsageMode 属性用于执行相同的操作。该博客描述了处理嵌套控件时 DesignMode 属性的另一个缺点。显然,UsageMode 属性没有同样的缺点,并且可以在构造函数中使用。我不能亲自保证这一点,但可能值得研究一下。

As others have stated, you can use the DesignMode property of the Component class. However, you will not be able to do this in the constructor of your control. The DesignMode property is always false in the constructor and methods called by the constructor. To get around this, re-factor your code to connect to the database in the OnLoad() callback. The DesignMode property is valid at that point. See here for the reasoning (look for the DesignMode section of the article).

I just ran across this blog entry that describes how to use the System.ComponentModel.LicenseManager.UsageMode property for doing the same thing. The blog describes an additional shortcoming of the DesignMode property when dealing with nested controls. Apparently, the UsageMode property doesn't have the same shortcomings and is available for use in the constructor. I cannot personally vouch for it, but might be worthwhile looking into.

临走之时 2024-08-19 11:40:24

在 Windows 窗体中?

if (!DesignMode)
{
    // code that shouldn't be executed at design time
}

正如其他人提到的,这在构造函数中不起作用。它经常在 Form.Load 事件中使用。

In Windows Forms?

if (!DesignMode)
{
    // code that shouldn't be executed at design time
}

As others have mentioned, this won't work in the constructor. It's often used in the Form.Load event.

ˇ宁静的妩媚 2024-08-19 11:40:24

我喜欢 Michael Petrotta 的 Windows 窗体方法。如果有人想将相同的技术应用于 WPF,只需使用 IsInDesignMode 即可。

例子:

public SomeViewModel()
{
    if (!IsInDesignMode)
    {
        DoWork();
    }
}

I liked Michael Petrotta's approach for Windows Forms. If anyone wants to apply the same technique to WPF, simply use IsInDesignMode.

Example:

public SomeViewModel()
{
    if (!IsInDesignMode)
    {
        DoWork();
    }
}
情话难免假 2024-08-19 11:40:24
public SomeViewModel()
{
    if (!IsInDesignMode)
    {
        DoWork();
    }
}

如果您正在处理您正在尝试处理的实际 UI,请使用上面的代码。如果控件上有类似的内容,当您切换回该控件的设计器时,就可以了,并且没有设计时错误。现在,如果您通过从工具箱中拖动包含上述代码的控件将其添加到其他窗体或另一个控件,它将显示一些设计时错误。

public SomeViewModel()
{
    if (!IsInDesignMode)
    {
        DoWork();
    }
}

This code above if you are working on the actual UI that you are trying to work on. In a situation that you have something like this on a control, when you switch back to the designer for that control it's ok and no design time error. Now if you added that control that contains the code above to some other Form or another control via dragging it from the toolbox, it will show some design time errors.

穿透光 2024-08-19 11:40:24

这是唯一对我有用的代码,在 Windows 窗体上使用 WPF UserControl。

private bool? inDesignMode;
public bool IsDesignMode
{
  get
  {
    if (inDesignMode == null)
    {
      var prop = System.ComponentModel.DesignerProperties.IsInDesignModeProperty;

      inDesignMode = (bool)System.ComponentModel.DependencyPropertyDescriptor
          .FromProperty(prop, typeof(FrameworkElement))
          .Metadata.DefaultValue;

      if (!inDesignMode.GetValueOrDefault(false) && System.Diagnostics.Process.GetCurrentProcess().ProcessName.StartsWith("devenv", StringComparison.Ordinal))
        inDesignMode = true;
    }

    return inDesignMode.GetValueOrDefault(false);
  }
}

This is the only code that worked for me, using WPF UserControl on a Windows Form.

private bool? inDesignMode;
public bool IsDesignMode
{
  get
  {
    if (inDesignMode == null)
    {
      var prop = System.ComponentModel.DesignerProperties.IsInDesignModeProperty;

      inDesignMode = (bool)System.ComponentModel.DependencyPropertyDescriptor
          .FromProperty(prop, typeof(FrameworkElement))
          .Metadata.DefaultValue;

      if (!inDesignMode.GetValueOrDefault(false) && System.Diagnostics.Process.GetCurrentProcess().ProcessName.StartsWith("devenv", StringComparison.Ordinal))
        inDesignMode = true;
    }

    return inDesignMode.GetValueOrDefault(false);
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文