从Control的构造函数中检测设计模式

nwa*_*aet 94 c# winforms

这个问题之后,是否可以从对象的构造函数中检测一个是处于设计模式还是运行时模式?

我意识到这可能是不可能的,而且我将不得不改变我想要的东西,但是现在我对这个具体问题很感兴趣.

adr*_*nks 176

您可以在命名空间中使用LicenceUsageMode枚举System.ComponentModel:

bool designMode = (LicenseManager.UsageMode == LicenseUsageMode.Designtime);
Run Code Online (Sandbox Code Playgroud)

  • @Filip Kunc:如果这在OnPaint中不起作用,您可以在构造函数中检查这个条件并将其存储在类字段中. (7认同)
  • 在用户控件中覆盖WndProc时,这也不起作用.必须使用@IMil建议 (3认同)
  • 如果在用户控件 A 中使用“LicenseManager.UsageMode”,并且此 UC A 在另一个程序集中的另一个用户控件或表单 B 中使用,则此方法不起作用。在这种情况下,我仍然得到“Runtime”而不是“DesignTime”。 (3认同)
  • 优雅的解决方案,它比C#功能`ISite.DesignMode`更好. (2认同)
  • 在OnPaint中,Me.DesignMode似乎有效 (2认同)

Jar*_*rek 22

你在寻找这样的东西:

public static bool IsInDesignMode()
{
    if (Application.ExecutablePath.IndexOf("devenv.exe", StringComparison.OrdinalIgnoreCase) > -1)
    {
        return true;
    }
    return false;
}
Run Code Online (Sandbox Code Playgroud)

您也可以通过检查进程名称来执行此操作:

if (System.Diagnostics.Process.GetCurrentProcess().ProcessName == "devenv")
   return true;
Run Code Online (Sandbox Code Playgroud)

  • 恕我直言,这看起来像一个丑陋的解决方案. (14认同)
  • 虽然我确信这在大多数用例中都能正常工作,但这个解决方案有一个主要缺陷:Visual Studio(至少在理论上)并不是唯一的设计主机.因此,只有当您的设计器由名为`devenv`的应用程序托管时,此解决方案才有效. (7认同)
  • 适用于OnPaint,派生类,构造函数等.最佳解决方案. (4认同)
  • 注意这里可能存在内存泄漏.必须处理流程. (4认同)
  • 适用于VS2013,与目前接受的答案不同. (2认同)

Vac*_*ara 9

组件......据我所知,没有DesignMode属性.此属性由Control提供.但问题是当CustomControl位于设计器的Form中时,此CustomControl在运行时模式下运行.

我经历过,DesignMode属性仅在Form中正常工作.

  • +1你说得对,这也是我的经验。当您将用户控件放置在窗体上时,如果有任何 mouseenter 或 load 事件,DesignMode 仍将显示为 false,因为您不处于该控件的设计模式。根据我的经验,它会导致 Visual Studio 严重崩溃。 (2认同)

for*_*atc 7

控件(Forms,UserControls等)继承了以下Component class内容bool property DesignMode:

if(DesignMode)
{
  //If in design mode
}
Run Code Online (Sandbox Code Playgroud)

  • 构造函数运行时未设置,又名OP的初始发行。可以使用它的第一刻是在OnHandleCreated中。 (2认同)

Bea*_*uty 7

重要

使用Windows 窗体WPF有所不同!

他们有不同的设计师,需要不同的检查.另外,当你混合使用Forms和WPF控件时,它很棘手.(例如窗体窗口内的WPF控件)

如果您只有Windows 窗体,请使用:

Boolean isInWpfDesignerMode   = (LicenseManager.UsageMode == LicenseUsageMode.Designtime);
Run Code Online (Sandbox Code Playgroud)

如果您只有WPF,请使用此检查:

Boolean isInFormsDesignerMode = (System.Diagnostics.Process.GetCurrentProcess().ProcessName == "devenv");
Run Code Online (Sandbox Code Playgroud)

如果您混合使用 Forms和WPF,请使用如下检查:

Boolean isInWpfDesignerMode   = (LicenseManager.UsageMode == LicenseUsageMode.Designtime);
Boolean isInFormsDesignerMode = (System.Diagnostics.Process.GetCurrentProcess().ProcessName == "devenv");

if (isInWpfDesignerMode || isInFormsDesignerMode)
{
    // is in any designer mode
}
else
{
    // not in designer mode
}
Run Code Online (Sandbox Code Playgroud)

要查看当前模式,您可以显示MessageBox以进行调试:

// show current mode
MessageBox.Show(String.Format("DESIGNER CHECK:  WPF = {0}   Forms = {1}", isInWpfDesignerMode, isInFormsDesignerMode));
Run Code Online (Sandbox Code Playgroud)

备注:

您需要添加名称空间System.ComponentModelSystem.Diagnostics.


Ula*_*kar 5

您应该使用Component.DesignMode属性.据我所知,这不应该从构造函数中使用.

  • 当您的控件位于正在设计的另一个控件或窗体内时,这不起作用. (6认同)