“设计模式”预处理器指令
我在显示组件时遇到问题在设计师中。
我发现了设计者不喜欢的“坏”代码。
现在,问题是我无法仅使用预处理器指令在设计时“注释”它。
现在,我尝试了(对于 VB.NET)以下操作
#If Not Debug Then
Private Sub myWpfComponent_ItsEvent(sender, args) Handles myWpfComponent.ItsEvent
...
#End If
...有效,现在它在设计器中显示没有问题。
现在的问题是我恐怕无法正确调试这个组件。
所以,我正在寻找一种解决方法,
#If Not DESIGN_TIME Then
#End If
有类似的东西吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您无法通过预处理器实现这一点。这是因为您可以在 VS 之外运行调试可执行文件(尝试一下,在调试模式下双击 VS 生成的 EXE)。
无论如何,有一个运行时(不是基于预处理器的)属性可能会有所帮助:
这些网页将提供帮助,并具有在运行时检查设计模式的其他方法:
http://msdn.microsoft.com/en-us/library/c58hb4bw(vs.71).aspx
http://weblogs.asp.net/fmarguerie/archive/2005/03/ 23/395658.aspx
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:
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
IDE 不会重建您的代码来向设计者展示。它使用您已经构建的二进制文件。所以预处理器指令不会有帮助。
既然您提到了 myWpfComponent_ItsEvent,我认为这是一个 WPF 问题。在 WPF 中,您可以使用 GetIsInDesignMode。
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.使用:
注意“this”标识符可以是任何DependencyObject
Use:
Note that "this" identifier can be any DependencyObject
您的问题是在 WinForms 设计器中使用用 VB.NET 编写的 WPF 控件。如果事件处理程序导致问题,您可以使用
AddHandler
而不是WithEvents
和Handles
来条件化处理程序代码。使用AddHandler
后,您可以使用 @gmagana 的答案中描述的方法将处理程序添加到If
中。有关 Handles 和 AddHandler 之间的区别,请参阅此答案:
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 ofWithEvents
andHandles
to conditionalize your handler code. Once you are usingAddHandler
you can wrap adding the handler in aIf
using the methods described in @gmagana's answer.See this answer for the difference between Handles and AddHandler: