“Design Mode” preprocessor directive

六眼飞鱼酱① 提交于 2019-12-01 14:30:58

问题


I have a problem on displaying a component in Designer.

I identified the "bad" code that the designer does not like.

Now, the problem is that I can't "comment" it for design time only using preprocessor directives.

Now, I tried (for VB.NET) the following

#If Not Debug Then
Private Sub myWpfComponent_ItsEvent(sender, args) Handles myWpfComponent.ItsEvent
...
#End If 

this... worked, and now it is displayed without problems in the designer.

The problem now that I am afraid do not be able to debug properly this component.

So, I am searching for a workaround à la

#If Not DESIGN_TIME Then
#End If 

Is there something similar?


回答1:


You cannot achieve this through the preprocessor. This is because you can run a debug executable outside of VS (try it, double click on the EXE generated by VS under debug mode).

Anyway, there is a runtime (not preprocessor based) property that might help:

if (System.ComponentModel.LicenseManager.UsageMode ==
    System.ComponentModel.LicenseUsageMode.Designtime)

These web pages will help and have other methods of checking for design mode at runtime:

http://msdn.microsoft.com/en-us/library/c58hb4bw(vs.71).aspx

http://weblogs.asp.net/fmarguerie/archive/2005/03/23/395658.aspx




回答2:


The IDE doesn't rebuild your code to show the designer. It uses the binary that you've already built. So a preprocessor directive won't help.

Since you mention myWpfComponent_ItsEvent, I assume this is a WPF question. In WPF, you detect design mode by using GetIsInDesignMode.




回答3:


Use:

if (!DesignerProperties.GetIsInDesignMode(this))
{
   //Code to not execute in design mode
}

Note that "this" identifier can be any DependencyObject




回答4:


Your problem is using a WPF control written in VB.NET in the WinForms designer. If the event handler is causing problems, you can use AddHandler instead of WithEvents and Handles to conditionalize your handler code. Once you are using AddHandler you can wrap adding the handler in a If using the methods described in @gmagana's answer.

See this answer for the difference between Handles and AddHandler:

  • Handles vs. AddHandler


来源:https://stackoverflow.com/questions/4821682/design-mode-preprocessor-directive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!