简体   繁体   English

VS 2008设计器和用户控制

[英]VS 2008 designer and usercontrol

I have created a custom data grid control. 我创建了一个自定义数据网格控件。 I dragged it on windows form and set its properties like column and all & ran the project. 我将其拖动到Windows窗体上并设置其属性(例如column和all)并运行该项目。 It built successfully and I am able to view the grid control on the form. 它构建成功,我可以在窗体上查看网格控件。

Now if i try to view that form in designer, I am getting following error.. 现在,如果我尝试在设计器中查看该表单,则会出现以下错误。

Object reference not set to an instance of an object.     




Instances of this error (1)  

1.   Hide Call Stack 

at Microsoft.VisualStudio.Design.Serialization.CodeDom.XML.CodeDomXmlProcessor.GetMemberTargetObject(XmlElementData xmlElementData, String& member)
at Microsoft.VisualStudio.Design.Serialization.CodeDom.XML.CodeDomXmlProcessor.CreateAssignStatement(XmlElementData xmlElement)
at Microsoft.VisualStudio.Design.Serialization.CodeDom.XML.CodeDomXmlProcessor.XmlElementData.get_CodeDomElement()
at Microsoft.VisualStudio.Design.Serialization.CodeDom.XML.CodeDomXmlProcessor.EndElement(String prefix, String name, String urn)
at Microsoft.VisualStudio.Design.Serialization.CodeDom.XML.CodeDomXmlProcessor.Parse(XmlReader reader)
at Microsoft.VisualStudio.Design.Serialization.CodeDom.XML.CodeDomXmlProcessor.ParseXml(String xmlStream, CodeStatementCollection statementCollection, String fileName, String methodName)
at Microsoft.VisualStudio.Design.Serialization.CodeDom.VSCodeDomParser.OnMethodPopulateStatements(Object sender, EventArgs e)
at System.CodeDom.CodeMemberMethod.get_Statements()
at System.ComponentModel.Design.Serialization.TypeCodeDomSerializer.Deserialize(IDesignerSerializationManager manager, CodeTypeDeclaration declaration)
at System.ComponentModel.Design.Serialization.CodeDomDesignerLoader.PerformLoad(IDesignerSerializationManager manager)
at Microsoft.VisualStudio.Design.Serialization.CodeDom.VSCodeDomDesignerLoader.PerformLoad(IDesignerSerializationManager serializationManager)
at Microsoft.VisualStudio.Design.Serialization.CodeDom.VSCodeDomDesignerLoader.DeferredLoadHandler.Microsoft.VisualStudio.TextManager.Interop.IVsTextBufferDataEvents.OnLoadCompleted(Int32 fReload) 

If I ignore the exception, form appears blank with no sign of grid control on it. 如果我忽略该异常,则窗体显示为空白,没有网格控制的迹象。 However I can see the code for the grid in the designer file. 但是,我可以在设计器文件中看到网格的代码。

Any pointer on this would be a great help. 任何对此的指点都会有很大帮助。

I have customized grid for my custom requirements like I have added custom text box n all. 我已经为我的自定义要求定制了网格,就像我已经添加了所有自定义文本框一样。 I have defined 3 constructors 我定义了3个构造函数

public GridControl() 
public GridControl(IContainer container) 
protected GridControl(SerializationInfo info, StreamingContext context)

我遇到了同样的错误,检查您的Windows Form类是否继承自System.Windows.Forms.Form类,就像Form1:Form希望dis有所帮助!!

It sounds like a NullReferenceException is thrown in your control's default constructor (the one without parameters). 听起来您的控件的默认构造函数(没有参数的那个)中抛出了NullReferenceException Obviously, this exception is only thrown at design time since you say it works at runtime. 显然,此异常仅在设计时抛出,因为您说它在运行时起作用。 Do you perform any initialization code in this constructor, like data base calls or similar? 您是否在此构造函数中执行任何初始化代码,例如数据库调用或类似的代码? Or do you use any instances which might not be available at design time? 还是使用在设计时可能不可用的任何实例?

It looks like the form-designer is trying to initialize the control. 看起来表单设计者正在尝试初始化控件。 Yet, the property it is trying to initialize may have been removed from the UserControl. 但是,它试图初始化的属性可能已从UserControl中删除。 There are lots of way to troubleshoot this problem. 有很多方法可以解决此问题。 I recommend you debug the control in design-time. 我建议您在设计时调试控件。 It is the surest way to find the problem. 这是找到问题的最可靠方法。 Check out "MSDN Search" for "design-time control debugging" at http://social.msdn.microsoft.com/Search/en-US?query=design-time+control+debugging&ac=8 http://social.msdn.microsoft.com/Search/zh-CN?query=design-time+control+debugging&ac=8上查看“ MSDN Search”中的“设计时控制调试”。

I have this problem all the time...it sucks. 我一直有这个问题...很烂。

[Ramble(on)] [罗嗦了)]

Here is what I think I know: 这是我想知道的:

  1. When designing place the control on a form. 在设计时,将控件放在窗体上。 Build and refresh often..this will let you know what change caused the designer to barf. 经常进行构建和刷新..这将使您知道导致设计人员caused倒的变化。
  2. Close visual studio all the way an re-open....I cannot tell you how many times I have chased a designer error that was the designer being "stuck". 一直关闭Visual Studio,然后重新打开。...我无法告诉您多少次我追逐设计师错误是设计师被“卡住了”。
  3. It is very important that you understand : The designer is really, really stupid...like bag of rocks stupid. 了解这一点非常重要:设计师真的非常愚蠢……就像一堆笨蛋一样。
  4. Any public fields or properties of custom object types will almost always cause designer confusion*. 自定义对象类型的任何公共字段或属性几乎总是会引起设计者的困惑*。 I find the following attributes will clear up most of these problems: 我发现以下属性将清除大多数这些问题:

    [Browsable(false)] [可浏览(假)]

    [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]

  5. Public fields or properties without a default constructor will always cause designer confusion. 没有默认构造函数的公共字段或属性将始终引起设计者困惑。 When you drop a user control on a form the designer effectively creates the control..so any public object needs a clear creation path. 当您将用户控件放在窗体上时,设计人员将有效地创建控件。因此,任何公共对象都需要一条清晰的创建路径。 I have found that the easiest way around this (read hack) is keeping the non-trivial custom classes private and expose public properties as a facade. 我发现解决此问题(读黑客)的最简单方法是将非平凡的自定义类设置为私有,并将公共属性公开为外观。

-- Did I say restart visual studio because sometimes the designer is "stuck" on an error that doesn't exist ?..I hope I did. -我是否说过要重新启动Visual Studio,是因为有时设计人员“卡在”不存在的错误上?..我希望我这样做了。

[Ramble(off)] [漫步(关闭)]

I hope some of this helps.. 我希望其中的一些帮助。

*designer confusion: Instead of showing your controls the designer shows you a useless error message that might or might not include the dire warning that it is protecting you from code loss...blah, blah. *设计师的困惑:设计师没有显示控件,而是向您显示了一条无用的错误消息,该消息可能包含也可能不包含可怕的警告,它正在保护您免受代码丢失的困扰……等等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM