C# 从控件的构造函数检测设计模式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1166226/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Detecting design mode from a Control's constructor
提问by nwahmaet
Following-on from this question, is it possible to detect whether one is in design or runtime mode from within an object's constructor?
继this question之后,是否可以从对象的构造函数中检测一个人是处于设计模式还是运行时模式?
I realise that this may not be possible, and that I'll have to change what I want, but for now I'm interested in this specific question.
我意识到这可能是不可能的,我必须改变我想要的,但现在我对这个特定问题感兴趣。
采纳答案by adrianbanks
You can use the LicenceUsageModeenumeration in the System.ComponentModel
namespace:
您可以在命名空间中使用LicenceUsageMode枚举System.ComponentModel
:
bool designMode = (LicenseManager.UsageMode == LicenseUsageMode.Designtime);
回答by Jarek
Are you looking for something like this:
你在寻找这样的东西:
public static bool IsInDesignMode()
{
if (Application.ExecutablePath.IndexOf("devenv.exe", StringComparison.OrdinalIgnoreCase) > -1)
{
return true;
}
return false;
}
You can also do it by checking process name:
您也可以通过检查进程名称来做到这一点:
if (System.Diagnostics.Process.GetCurrentProcess().ProcessName == "devenv")
return true;
回答by Ula Krukar
You should use Component.DesignMode property. As far as I know, this shouldn't be used from a constructor.
您应该使用 Component.DesignMode 属性。据我所知,这不应该从构造函数中使用。
回答by Vaclav Svara
With cooperation of the designer... It can be used in Controls, Components, in all places
在设计师的合作下......它可以用于控件,组件,在任何地方
private bool getDesignMode()
{
IDesignerHost host;
if (Site != null)
{
host = Site.GetService(typeof(IDesignerHost)) as IDesignerHost;
if (host != null)
{
if (host.RootComponent.Site.DesignMode) MessageBox.Show("Design Mode");
else MessageBox.Show("Runtime Mode");
return host.RootComponent.Site.DesignMode;
}
}
MessageBox.Show("Runtime Mode");
return false;
}
MessageBox.Show(
lines should be removed. It only makes me sure it works correctly.
MessageBox.Show(
应删除行。它只会让我确定它可以正常工作。
回答by Vaclav Svara
Component ... as far as I know does not have the DesignMode property. This property is provided by Control. But the problem is when CustomControl is located in a Form in the designer, this CustomControl is running in runtime mode.
组件...据我所知没有 DesignMode 属性。此属性由控件提供。但问题是当 CustomControl 位于设计器中的 Form 中时,此 CustomControl 以运行时模式运行。
I have experienced that the DesignMode property works correct only in Form.
我经历过 DesignMode 属性只能在 Form 中正确工作。
回答by user492238
Another interesting method is described on that blog: http://www.undermyhat.org/blog/2009/07/in-depth-a-definitive-guide-to-net-user-controls-usage-mode-designmode-or-usermode/
该博客上描述了另一种有趣的方法:http: //www.undermyhat.org/blog/2009/07/in-depth-a-definitive-guide-to-net-user-controls-usage-mode-designmode-or -用户模式/
Basically, it tests for the executing assembly being statically referenced from the entry assembly. It circumvents the need to track assembly names ('devenv.exe', 'monodevelop.exe'..).
基本上,它测试从入口程序集静态引用的执行程序集。它避免了跟踪程序集名称(“devenv.exe”、“monodevelop.exe”...)的需要。
However, it does not work in all other scenarios, where the assembly is dynamically loaded (VSTO being one example).
但是,它不适用于动态加载程序集的所有其他场景(VSTO 就是一个示例)。
回答by formatc
Controls(Forms, UserControls etc.) inherit Component class
which has bool property DesignMode
so:
Controls(Forms, UserControls etc.) 继承Component class
它有bool property DesignMode
这样的:
if(DesignMode)
{
//If in design mode
}
回答by Rob
The LicenseManager solution does not work inside OnPaint, neither does this.DesignMode. I resorted to the same solution as @Jarek.
LicenseManager 解决方案在 OnPaint 中不起作用,this.DesignMode 也不起作用。我采用了与@Jarek 相同的解决方案。
Here's the cached version:
这是缓存版本:
private static bool? isDesignMode;
private static bool IsDesignMode()
{
if (isDesignMode == null)
isDesignMode = (Process.GetCurrentProcess().ProcessName.ToLower().Contains("devenv"));
return isDesignMode.Value;
}
Be aware this will fail if you're using any third party IDE or if Microsoft (or your end-user) decide to change the name of the VS executable to something other than 'devenv'. The failure rate will be very low, just make sure you deal with any resulting errors that might occur in the code that fails as a result of this and you'll be fine.
请注意,如果您使用任何第三方 IDE,或者如果 Microsoft(或您的最终用户)决定将 VS 可执行文件的名称更改为“devenv”以外的名称,这将失败。失败率将非常低,只要确保您处理了因此而失败的代码中可能出现的任何结果错误,您就可以了。
回答by Beauty
IMPORTANT
重要的
There is a difference of using Windows Formsor WPF!!
使用 Windows窗体或WPF是有区别的!!
They have different designers and and need different checks. Additionally it's tricky when you mix Forms and WPF controls. (e.g. WPF controls inside of a Forms window)
他们有不同的设计师,需要不同的检查。此外,混合 Forms 和 WPF 控件时也很棘手。(例如窗体窗口内的 WPF 控件)
If you have Windows Forms only, use this:
如果您只有 Windows窗体,请使用以下命令:
Boolean isInWpfDesignerMode = (LicenseManager.UsageMode == LicenseUsageMode.Designtime);
If you have WPF only, use this check:
如果您只有WPF,请使用此检查:
Boolean isInFormsDesignerMode = (System.Diagnostics.Process.GetCurrentProcess().ProcessName == "devenv");
If you have mixed usageof Forms and WPF, use a check like this:
如果您混合使用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
}
To see the current mode you can show a MessageBox for debugging:
要查看当前模式,您可以显示用于调试的 MessageBox:
// show current mode
MessageBox.Show(String.Format("DESIGNER CHECK: WPF = {0} Forms = {1}", isInWpfDesignerMode, isInFormsDesignerMode));
Remark:
评论:
You need to add the namespaces System.ComponentModeland System.Diagnostics.
您需要添加命名空间System.ComponentModel和System.Diagnostics。
回答by Giovanny Farto M.
If you want to run some lines when it is running but not in the Visual Studio designer, you should implement the DesignMode property as follows:
如果要在运行时运行某些行但不在 Visual Studio 设计器中运行,则应按如下方式实现 DesignMode 属性:
// this code is in the Load of my UserControl
if (this.DesignMode == false)
{
// This will only run in run time, not in the designer.
this.getUserTypes();
this.getWarehouses();
this.getCompanies();
}