如何在构造函数中包含 Visual Studio 在设计时不会执行的代码?
我的用户控件的构造函数中有一个方法调用,该方法在设计时无法正常工作(连接到数据库),当我尝试将该控件添加到 GUI 设计器时,Visual Studio 就退出了。
当然,我可以将该代码分解为一个单独的方法,但我不喜欢这样的想法:每次使用该对象时,我都需要记住执行对该对象的功能至关重要的某个方法(这就是构造函数)为了!)。
是否有类似预处理器符号之类的东西可以用来标记我的代码,以便 Visual Studio 不会在设计时尝试执行该代码?
I have a method call in the constructor of my user control that does something that won't work at design time (connecting to a database), and Visual Studio just bailed out when I tried to add that control to the GUI designer.
Sure, I can factor out that code to a separate method, but I don't like the idea that every time I use that object I need to remember to execute a certain method which is essential to that object's function (that's what the constructor is for!).
Is there something like a preprocessor symbol that I can mark my code with so that Visual Studio won't try to execute that code at design time?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
正如其他人所说,您可以使用
DesignMode
属性。但是,您将无法在控件的构造函数中执行此操作。在构造函数和构造函数调用的方法中,Component
类的DesignMode
属性始终为false
。要解决此问题,请重构代码以在OnLoad()
回调中连接到数据库。DesignMode
属性此时有效。请参阅此处了解原因(查找文章的 DesignMode 部分) 。我刚刚遇到这个博客条目,它描述了如何使用
System.ComponentModel.LicenseManager.UsageMode
属性用于执行相同的操作。该博客描述了处理嵌套控件时DesignMode
属性的另一个缺点。显然,UsageMode
属性没有同样的缺点,并且可以在构造函数中使用。我不能亲自保证这一点,但可能值得研究一下。As others have stated, you can use the
DesignMode
property of theComponent
class. However, you will not be able to do this in the constructor of your control. TheDesignMode
property is alwaysfalse
in the constructor and methods called by the constructor. To get around this, re-factor your code to connect to the database in theOnLoad()
callback. TheDesignMode
property is valid at that point. See here for the reasoning (look for the DesignMode section of the article).I just ran across this blog entry that describes how to use the
System.ComponentModel.LicenseManager.UsageMode
property for doing the same thing. The blog describes an additional shortcoming of theDesignMode
property when dealing with nested controls. Apparently, theUsageMode
property doesn't have the same shortcomings and is available for use in the constructor. I cannot personally vouch for it, but might be worthwhile looking into.在 Windows 窗体中?
正如其他人提到的,这在构造函数中不起作用。它经常在
Form.Load
事件中使用。In Windows Forms?
As others have mentioned, this won't work in the constructor. It's often used in the
Form.Load
event.看一下这个
组件。 DesignMode 属性
Have a look at this
Component.DesignMode Property
我喜欢 Michael Petrotta 的 Windows 窗体方法。如果有人想将相同的技术应用于 WPF,只需使用
IsInDesignMode
即可。例子:
I liked Michael Petrotta's approach for Windows Forms. If anyone wants to apply the same technique to WPF, simply use
IsInDesignMode
.Example:
如果您正在处理您正在尝试处理的实际 UI,请使用上面的代码。如果控件上有类似的内容,当您切换回该控件的设计器时,就可以了,并且没有设计时错误。现在,如果您通过从工具箱中拖动包含上述代码的控件将其添加到其他窗体或另一个控件,它将显示一些设计时错误。
This code above if you are working on the actual UI that you are trying to work on. In a situation that you have something like this on a control, when you switch back to the designer for that control it's ok and no design time error. Now if you added that control that contains the code above to some other Form or another control via dragging it from the toolbox, it will show some design time errors.
这是唯一对我有用的代码,在 Windows 窗体上使用 WPF UserControl。
This is the only code that worked for me, using WPF UserControl on a Windows Form.