WinForms 设计器异常
当我尝试在设计模式下显示包含 UserControl 的 WinForms 窗体时,它会引发异常,但在运行或调试程序时可以正常运行。
设计师说:
变量“fpInfoA”未声明或从未分配。
ResearchTool fMain.Designer.cs 行:282 列:1 调用堆栈
在 System.ComponentModel.Design.Serialization.CodeDomSerializerBase.Error(IDesignerSerializationManager 管理器,字符串 exceptionText,字符串 helpLink) 在 System.ComponentModel.Design.Serialization.CodeDomSerializerBase.DeserializeExpression(IDesignerSerializationManager 管理器,字符串名称,CodeExpression 表达式) 在 System.ComponentModel.Design.Serialization.CodeDomSerializerBase.DeserializeExpression(IDesignerSerializationManager 管理器,字符串名称,CodeExpression 表达式) 在 System.ComponentModel.Design.Serialization.CodeDomSerializerBase.DeserializeStatement(IDesignerSerializationManager 管理器,CodeStatement 语句)
但是,变量看起来像我在 InitializeComponent
中所期望的那样分配了
private void InitializeComponent()
{
// ... (Order of statements is same as in actual code) ...
this.tpFpA = new System.Windows.Forms.TabPage();
this.fpInfoA = new ResearchTool.FPInfo();
// ...
this.tpFpA.Controls.Add(this.fpInfoA); // THIS LINE BLOWS UP IN DESIGN MODE
}
关于如何追踪此问题的想法?例如,有没有办法调试设计器的初始化?
A WinForms form that includes a UserControl throws an exception when I attempt to display it in design mode, but runs properly when the program is ran or debugged.
The designer says:
The variable 'fpInfoA' is either undeclared or was never assigned.
ResearchTool fMain.Designer.cs Line:282 Column:1
Call Stack
at System.ComponentModel.Design.Serialization.CodeDomSerializerBase.Error(IDesignerSerializationManager manager, String exceptionText, String helpLink)
at System.ComponentModel.Design.Serialization.CodeDomSerializerBase.DeserializeExpression(IDesignerSerializationManager manager, String name, CodeExpression expression)
at System.ComponentModel.Design.Serialization.CodeDomSerializerBase.DeserializeExpression(IDesignerSerializationManager manager, String name, CodeExpression expression)
at System.ComponentModel.Design.Serialization.CodeDomSerializerBase.DeserializeStatement(IDesignerSerializationManager manager, CodeStatement statement)
However, it looks like the variable is assigned as I would expect in InitializeComponent
private void InitializeComponent()
{
// ... (Order of statements is same as in actual code) ...
this.tpFpA = new System.Windows.Forms.TabPage();
this.fpInfoA = new ResearchTool.FPInfo();
// ...
this.tpFpA.Controls.Add(this.fpInfoA); // THIS LINE BLOWS UP IN DESIGN MODE
}
Thoughts on how to track down this issue? For example, is there a way to debug initialization of the designer?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您无法解决问题,一种解决方法是通过检查
DesignMode
来包围有问题的代码位。例如:
如果它正在执行设计模式中不需要的操作并且速度相当慢,例如连接到数据库或类似的操作,这也可以稍微加快速度。
One workaround in case you can't fix the issue, would be to surround the offending bits of code with checks for
DesignMode
.As in:
This can also speed it up a little bit if it's doing things that aren't needed in design mode and that are quite slow, like connecting to databases or similar.
正如 Hans Olsson 所说,这个问题可以通过检查设计模式并禁用违规逻辑来解决。
如果
UserControl
的构造函数存在任何问题,也会触发此错误。如果设计器实例化您的UserControl
时发生异常,设计器将会失败。就我而言,失败导致了相同的“[...] 要么未声明,要么从未分配”错误。例如,请参阅以下用户控件:
现在,当观察包含此
MyUserControl
的表单的设计器时,我们将看到类似于以下内容的内容: sstatic.net/rAFVY.png" rel="nofollow noreferrer">
我不能说是否设计师是这样的Visual Studio 的早期版本;但对于 Visual Studio 2017,你可以清楚地看到发生了什么。
设计器失败,因为引发了
System.Exception
。因此,变量[REDACTED]
被认为未声明或从未分配,而实际上自动生成的设计器代码是正确的。问题出在MyUserControl
的构造函数上。现在,如果您需要将依赖于外部服务/资源的逻辑放入控件的构造函数中,则需要指示它应该仅在运行时发生。或者,您可以为设计时提供模型资源。
为此,您可以使用
LicenseManager
并检查其当前的UsageMode
。下面修改后的代码现在只在运行时抛出异常,设计者不再有错误。
As Hans Olsson said, this potentially could be resolved by checking for design-mode and disabling offending logic.
This error will also trigger if there is any issue with the constructor of your
UserControl
. If there is an exception caused when the designer instantiates yourUserControl
, the designer will fail. In my case, the failure resulted in the same "[...] is either undeclared or was never assigned" error.For example, see the following user control:
Now, when observing the designer for a form that contains this
MyUserControl
, we will see something similar to the following:I cannot say if the designer is like this for previous versions of Visual Studio; but as for Visual Studio 2017, you can clearly see what happened.
The designer failed because a
System.Exception
was thrown. As a result, the variable[REDACTED]
was thought to be undeclared or never assigned when in fact the auto-generated designer code was correct. The issue was with theMyUserControl
's constructor.Now, if you need to put logic that depends on external services/resources inside the control's constructor, you need to indicate that it should only occur during runtime. Alternatively, you can provide mock-up resources for design-time.
To do this, you can use the
LicenseManager
and check its currentUsageMode
.The modified code below only throws the exception in runtime now, and the designer doesn't have the error anymore.
您可以在以下位置找到有关如何跟踪设计时代码执行的信息:
什么信息您是否需要解决产品在设计时出现的问题?
You will find the information on how to trace design time code execution at:
What information do you need to fix a problem, which occurs with your products at design time?