在设计器中打开 UserControl 时 VisualStudio 中出现错误
背景: 我已经创建了这个用户控件。在用户控件的构造函数中,我调用一个从数据库检索一些值的函数。如果检索值时发生错误,则会显示解释该错误的消息框。到目前为止,一切都很好。
问题: 我创建了一个包含我的用户控件(以及其他元素)的表单。现在,当我打开此表单(甚至是 UserControl 本身)时,将调用构造函数(我想这样可以准确地绘制它),并且由于数据库不可用,因此会显示消息框(如上所述)。
我该如何防止这种情况发生?
我只是想澄清一下:代码在运行时运行得很好。一切都如设计的那样。仅在 Visual Studio(2008 SP1,如果重要的话)的设计器视图中才会出现问题。然而,在 Designer 中,这很糟糕,尤其是现在当应用程序在连接失败时尝试重新连接时。每次我进入设计器模式时,我的 Visual Studio 都会冻结大约 20 秒(重新连接超时),并且它会终止我的工作进程。
Background:
I have created this UserControl. In the constructor of the user control I call a function that retrieves some values from the database. If an error occurs while retrieving the values a messagebox explaining the error is shown. So far so good.
The Problem:
I have created a form that (among other elements) includes my UserControl. Now when I open this form (or even the UserControl itself) the constructor is called (I suppose so it can be drawn accurately) and, because the database is unavailable, the messagebox (explained above) is shown.
How do I prevent this from happening?
I just want to be clear: the code works great in runtime. Everything is as designed. It is only in Designer view of Visual Studio (2008 SP1 if it matters) where the problem occurs. However in Designer it is awful, especially now when the application attempts to reconnect when the connection fails. Every time I go in to Designer mode my Visual Studio freezes for about 20 seconds (timeout of the reconnect) and it is killing my work process.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

发布评论
评论(2)
我通过在我的 Program 类上添加一个名为 IsRunning 的全局静态属性来解决这个问题。
当我的程序在 main 方法中启动时,我将 IsRunning 属性设置为 true。然后在我的用户控件的构造函数中,我可以轮询属性 IsRunning 以确定我是否执行特定代码,在您的情况下代码将尝试访问数据库...
编辑:
这是一些代码......
private static bool _running = false;
/// <summary>
/// Gets or sets a value indicating whether this <see cref="Program"/> is running.
/// This property is used for design time WSOD issues and is used instead of the
/// DesignMode property of a control as the DesignMode property has been said to be
/// unreliable.
/// </summary>
/// <value><c>true</c> if running; otherwise, <c>false</c>.</value>
public static bool Running
{
get
{
return _running;
}
}
static void Main(string[] args)
{
Initialize();
_running = true;
在
我的用户控件中......
public AssignmentList()
{
InitializeComponent();
if (!Program.Running)
{
return;
}
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
您可以检查您的控件是否以设计模式显示:
http://msdn.microsoft.com/en-us/library/system.componentmodel.component.designmode.aspx
/Edit:正如人们在另一个答案的评论中指出的那样,DesignMode 属性在构造函数。因此,最好的解决方案可能是将数据库内容移至“Load”等事件中,并在那里使用 DesignMode 属性。
You could check whether your control is displayed in design mode:
http://msdn.microsoft.com/en-us/library/system.componentmodel.component.designmode.aspx
/Edit: As people pointed out in the comments to another answer, the DesignMode property is not usable in the constructor. So the best solution would probably be to move the database stuff into an event like "Load" and use the DesignMode property there.