Visual Studio WinForms设计器不实例化对象 [英] Visual Studio WinForms designer does not instantiate object

查看:91
本文介绍了Visual Studio WinForms设计器不实例化对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个派生自System.Windows.Forms.ContextMenuStrip类的类,而不是作为用户控件,而是一个带有构造函数和一个事件处理程序的普通.cs类。

I created a class derived from System.Windows.Forms.ContextMenuStrip class, not as a user control, just a plain .cs class with a constructor and one event handler.

当我将此类从工具箱拖到设计器上时,它将为其创建一个私有成员和几个属性,但不会实例化对象。

When I drag this class from the Toolbox onto the designer, it creates a private member for it and a couple of properties, but does not instantiate an object.

因此,在运行时我得到对象引用未设置为对象的实例。,因为设计器从不生成以下行:

Thus, at runtime I get "Object reference not set to an instance of an object.", because the designer never generates the line:

this.searchGridContextMenu1 = new SearchGridContextMenu( );

this.searchGridContextMenu1 = new SearchGridContextMenu();

在InitializeComponent内部。

inside InitializeComponent.

它曾经生成此行,事实上,我一直将其从我的Vault存储库中放回去,但设计者只是再次吃掉了。

It used to generate this line, as a matter of fact, I keep putting it back in from my Vault repository, but the designer just "eats it" again.

更新:我现在尝试使用相同的类创建用户控件,

Update: I now tried creating a User Control using the same class and it has the same problem just doing that.

推荐答案

我在另一个问题上交叉发表了此评论,但胜诉

I cross-posted this comment on another question but since this is related here it is again.

当用户控件无法加载到Visual Studio设计器中时,这是您需要做的。这些说明适用于vb.net项目,但c#应该相似。此外,在执行此操作之前,请关闭所有打开的窗口(或至少关闭正在处理的控件的源文件和设计器文件。)

When a User Control won't load into the Visual Studio designer here is what you need to do. These instruction are for vb.net project but c# should be similar. Also, before doing this close all open windows (or at least the source and designer files of the control you are working on.)

最后一件事。您首先要做的是确保重新启动Visual Studio无法解决问题。如果没有,您可以尝试以下步骤。这些说明假定错误的用户控件在Visual Studio的控件库项目中。如果不是这样,您应该可以稍微调整一下方向使其工作,但是当控件位于其自己的项目中时,这会容易得多。

One last thing. The FIRST thing you should do is ensure that restarting visual studio doesn't fix the problem. If not you can try the steps that follow. These instructions assume that the errant user controls are in control library project in visual studio. If not you should be able to adjust the directions a bit to get it to work but it is much easier when the control is in its own project.

执行以下操作:


  1. 将控件库设为启动项目。

  2. 打开控件库项目的属性,然后单击调试选项卡。

  3. 在开始操作下,单击启动外部程序选项,然后浏览到Visual Studio可执行文件。

注意:这意味着在您运行解决方案时,它将启动Visual Studio的另一个实例,而不是实际运行您的解决方案。运行Visual Studion的第一个实例(INSTANCE_1)将承载 Visual Studio的第二个实例(INSTANCE_2)。

NOTE: what this means is that when you run your solution it will fire up another instance of Visual Studio instead of actually running your solution. The First Instance of Visual Studion (INSTANCE_1) will "host" a second instance of visual studio (INSTANCE_2) when you run it.


  1. 运行您的解决方案。将会加载INSTANCE_2。

  2. 切换回INSTANCE_1。

  3. 在INSTANCE_1中,按CTRL-ALT-E。这将打开例外对话框。选中公共语言运行时异常旁边的THROWN列复选框。

注意:这将确保INSTANCE_1会在任何运行时错误时中断。

NOTE: This will ensure that INSTANCE_1 will BREAK at ANY runtime error even if it is hit in a try block.


  1. 切换到INSTANCE_2。在解决方案资源管理器中,双击以打开错误的用户控件。

您应该发现Visual Studio的INSTANCE_1应该已经停止在导致设计者无法加载控件的代码行中。修复代码(这通常意味着在引用对象属性之前测试IsNot Nothing ...但可能意味着其他事情。)

You should find that INSTANCE_1 OF Visual Studio should have stopped at the line of code that caused the designer to not load the control. Fix the code (which usually means testing for IsNot Nothing before references an object properties...but could mean other things.)

此外,有时我会发现控件将加载到INSTANCE_2中,而不是因为发生INSTANCE_1错误而中断。在这种情况下,只需停止调试...关闭INSTANCE_2。保存/重新启动INSTANCE_1,您的问题通常就会消失。

Also, sometimes I find that the control WILL load in INSTANCE_2 instead of breaking on an error in INSTANCE_1. In that case just stop debugging...close INSTANCE_2. Save/Restart INSTANCE_1 and your problem will often have gone away.

这是本课。用户控件必须能够加载/引用所有对象及其成员,以便将其加载到设计器中。因此,对于将放置在其他容器上的用户控件,我通常会设计事件以通知父控件,而不是尝试将对象推送到子控件中。

The lesson is this. User Control MUST be able to load/reference all objects and their members in order to load it into the designer. So for User Controls that will be placed onto other containers I will usually design events to notify the parent rather than trying to push objects into the child control.

希望这对以后这个老问题有帮助。

Hope this helps for future reference on this old question.

Seth

这篇关于Visual Studio WinForms设计器不实例化对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆