"设计模式 "预处理程序指令[英] "Design Mode" preprocessor directive

本文是小编为大家收集整理的关于"设计模式 "预处理程序指令的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

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?

推荐答案

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

其他推荐答案

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.

其他推荐答案

Use:

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

Note that "this" identifier can be any DependencyObject