哪些因素会触发 Visual Studio 重新生成 *.Designer.cs 文件?
我遇到的一些表单初始化问题可追溯到.Designer。 cs 文件覆盖我在用户控件的构造函数中初始化的值。
我设计了一个组件 (MyUserControl
) 并将其插入到表单 (MainForm
) 中。后来我在 MyUserControl
的构造函数中添加了一些成员初始化,但 MainForm.Designer.cs
中 InitializeComponent()
的代码已过时,该函数后面的代码覆盖了该函数之前调用的构造函数中的新初始化。
因此,本质上,我所做的事情触发了 Mainform.Designer.cs 的重写,但其他活动却没有。
是否有一个很好的参考来描述设计师何时接触 *.Designer.cs 文件? (或者这是一个简单的规则?)Jared Par 向我指出了 DesignerSerializationVisibilityAttribute 类,但这似乎是森林中的一棵树?
Some form initialization issues I was having were traced to the .Designer.cs file overwriting values I had initialized in a user control's constructor.
I had designed a component (MyUserControl
) and inserted it into a form (MainForm
). I later added some member initializations in MyUserControl
's constructor, but the code that was in MainForm.Designer.cs
for InitializeComponent()
was stale, and code later in that functions was overwriting the my new initializations from the constructor called earlier in that function.
So, in essence, something I did triggered a rewrite of Mainform.Designer.cs
but other activities didn't.
Is there a good reference that describes when the designer touches the *.Designer.cs files? (Or is it a simple rule?) Jared Par pointed me to the DesignerSerializationVisibilityAttribute class but that seems to be one tree in the forest?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您特别成为旧版本 UserControl 的代码仍然嵌入在表单的 InitializeComponent() 方法中的受害者。如果您在修改控件代码之前从表单中删除了该控件,那么您就不会遇到问题。后来把它拆下来再放回去也能解决问题。
是的,这是一个简单的规则。每次在表单中添加或删除控件时都会重新生成 InitializeComponent()。
You specifically fell victim to having code for the old version of your UserControl still embedded in the form's InitializeComponent() method. If you would have removed the control from the form before modifying the control code then you wouldn't have had the problem. Removing it afterwards and putting it back would have fixed it too.
Yes, it is a simple rule. InitializeComponent() is re-generated every time you add or remove a control to/from the form.