简体   繁体   English

为什么Visual Studio会从我的代码中删除静态内容?

[英]Why does visual studio remove static from my code?

I've manually set a label to a 'static', when I run the code it works fine but the next time I run the code it gives me a build error, this is because the 'static' got removed automatically. 我已经手动将标签设置为“静态”,当我运行代码时它可以正常工作,但是下次我运行代码时,它给了我一个构建错误,这是因为“静态”被自动删除了。 I edited this in the formMain.Designer.cs file. 我在formMain.Designer.cs文件中对此进行了编辑。

I looked on google and in Visual Studio itself but didn't find anything. 我在Google和Visual Studio本身上进行了查找,但未找到任何内容。

Here is a link to Paste-bin as the code would take to much space: https://pastebin.com/LkPwrJhY public static System.Windows.Forms.ToolStripStatusLabel lblSerialStatus; 这是粘贴到bin的链接,因为代码会占用很多空间: https : public static System.Windows.Forms.ToolStripStatusLabel lblSerialStatus;

The label I changed is on the last line of code, 'lblSerialStatus', at line 180. I'm changing this label because I want to change it from an other form. 我更改的标签位于代码的最后一行“ lblSerialStatus”的第180行。我正在更改此标签,因为我想将其从其他形式更改。

Thank you in advanced! 谢谢高级!

This is because formMain.Designer.cs file is auto-generated. 这是因为formMain.Designer.cs文件是自动生成的。 Meaning every time you change something in the visual designer this file gets re-generated over writing any manual changes done to the file 这意味着每次您在视觉设计器中进行更改时,都会通过将任何手动更改写入文件来重新生成该文件
If you really want the label somewhere else you'll need to pass it as a parameter to some method. 如果您确实希望将标签放在其他地方,则需要将其作为参数传递给某些方法。 Also, static UI elements make no sense, because the UI will not always be there or be accessible 另外,静态UI元素毫无意义,因为UI不会一直存在或无法访问

Changing Designer.cs file is not recommended. 不建议更改Designer.cs文件。 The file is automatically generated and it will be generated again whenever you change the corresponding forms file. 该文件是自动生成的,每当您更改相应的表单文件时,它将再次生成。

look before InitializeComponent() its also mentions in your generated designer Code InitializeComponent()之前先看看它在generated designer Code也提到过

#region Windows Form Designer generated code

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

If you want then you can create another file which contains a partial class where you can put all such logic or function 如果需要,则可以创建另一个文件,其中包含partial class ,可以在其中放置所有此类逻辑或函数。

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

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