简体   繁体   English

为什么Visual Studio Designer会删除代码?

[英]Why does the Visual Studio Designer delete code?

Several times throughout the course of our current project, the visual studio designer has made modifications that resulted in losing code. 在我们当前项目的整个过程中,Visual Studio设计师已多次进行修改,导致代码丢失。 For example, event handlers wirings that were set up manually in the designer code were lost. 例如,在设计器代码中手动设置的事件处理程序布线丢失了。 Does anyone know why this happens? 有谁知道为什么会这样?

Edit: I understand that editing these files manually is not a good idea, but it has also happened with other event wirings set up through the designer. 编辑:我知道手动编辑这些文件不是一个好主意,但它也发生在设计师设置的其他事件布线上。

Well for starters read the XML at the top of your designer.cs file. 对于初学者来说,请阅读designer.cs文件顶部的XML。

/// <summary>
/// Required method for Designer support - do not modify
/// contents of this method with the code editor.
/// </summary>
private void InitializeComponent()

Generally you shouldn't be modifying these files as they are auto-generated. 通常,您不应该修改这些文件,因为它们是自动生成的。 It's probably the reason why there is a slight attempt to hide the code within a branch, underneath the main partial class. 这可能是为什么在主要的分类下面隐藏代码的原因。

I have on occasion found that the process has removed its own auto-generated code that I've had to merge back in. Most commonly it decides it's not going to instantiate custom user controls anymore, so when I start running I get a NullReferenceException. 我偶尔发现该进程已经删除了自己生成的代码,我必须将其合并回来。最常见的是它决定不再实例化自定义用户控件,所以当我开始运行时,我得到一个NullReferenceException。

Really the answer is to put the code somewhere else, like in the constructor before calling the InitializeComponent() method. 答案是将代码放在其他地方,就像在构造函数中调用InitializeComponent()方法之前一样。 If fellow developers aren't aware of this, then you should inform them and educate them, the fact that the files are .designer.cs should raise questions even to newer developers as to why the strange extension. 如果开发人员没有意识到这一点,那么你应该通知他们并教育他们,文件是.designer.cs的事实应该向新开发者提出问题,为什么这个奇怪的扩展。

You guys aren't modifying generated code files, are you? 你们不是在修改生成的代码文件,不是吗? Like MyForm.Designer.cs ? 喜欢MyForm.Designer.cs This is why we were given partial classes. 这就是我们获得partial课程的原因。

Because it is designer generated and more or less maintained code. 因为它是设计器生成的或多或少维护的代码。 It is recommended that you not add or modify code in the designer partial class manually exactly because of the behavior you described (I think it even mentions this in the generated file itself). 由于您描述的行为,我们建议您不要手动添加或修改设计器分部类中的代码(我认为它甚至在生成的文件本身中提到了这一点)。 If you need to wire up event handlers manually then do it in your custom code possibly the constructor of your control. 如果您需要手动连接事件处理程序,请在您的自定义代码中执行它,可能是您的控件的构造函数。

只在你自己的.cs文件中写下所有必要的初始化代码,你有很多地方可以做到这一点,比如表单构造函数和表单Load事件处理程序。

Moving the designer code into user maintained code classes defeats the benefits of being able to modify the user interface. 将设计器代码移动到用户维护的代码类中会破坏能够修改用户界面的好处。 Perhaps this might be something you would want to do if the program never needs to be modified again, but if that is the case... where is the benefit as well? 如果程序永远不需要再次修改,也许这可能是你想要做的事情,但如果是这样的话......那么好处在哪里呢?

This issue of the designer class losing lines of code has existed since Visual Studio was initially released and has caused me countless nightmares because the loss occurred perhaps weeks before, when the program was previously modified. 自Visual Studio最初发布以来,设计者类失去代码行的问题已经存在,并且已经引起了无数的噩梦,因为损失发生在可能几周之前,当时程序之前被修改过。

I can't prove it but it seems obvious that something in Microsoft's code generator is failing and there isn't any alerts to tell you it crashed. 我无法证明这一点,但很明显微软的代码生成器中的某些内容失败了,并且没有任何警报告诉你它崩溃了。 So, Microsoft... fix what is crashing or at least, tell us about it when it happens. 所以,微软......修复什么是崩溃或至少,当它发生时告诉我们。

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

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