[英]When not using VS designer, should I add code to Form1.designer.cs?
I am not sure about that. 我不确定。 I do not want to use designer for many things (but for some I do). 我不想在很多事情上使用设计师(但对于某些人来说)。 Should I write my "manual" code into the designer.cs or to form class itself? 我应该将我的“手动”代码写入designer.cs还是形成类本身?
No, never. 没有永不。
Because it will be generated again and your written-code will be lost. 因为它将再次生成,您的书面代码将丢失。
In order to add code to a class which has designer, you need to define a partial class with the same name. 为了将代码添加到具有设计器的类中,您需要定义具有相同名称的部分类。
public partial class Form1
{
// here write your logic (which will be preserved).
}
For more info: Partial Class (C#) 有关更多信息: 部分类(C#)
You should really not do that. 你真的不应该那样做。 Each time you change something in the designer, it will regenerate the InitializeComponent()
method. 每次在设计器中进行更改时,它都会重新生成InitializeComponent()
方法。 Therefore, your changes will be overwritten by that code. 因此,您的更改将被该代码覆盖。 The only way this would not happen is to write your code in the same way the designer does - which is almost impossible. 唯一不会发生这种情况的方法就是以设计人员的相同方式编写代码-这几乎是不可能的。 It could also happen that your designer crashes after you changed the code and you may not know why. 更改代码后,设计师也可能崩溃,并且您可能不知道为什么。 You should better stick with the designer. 你应该更好地坚持设计师。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.