[英]Editing the InitializeComponent() method C#
I've gone through multiple resources on trying to find the use cases of when to manually add code to InitializeComponent, but haven't found anything concrete. 在尝试查找何时将代码手动添加到InitializeComponent的用例中,我已经遍历了很多资源,但是还没有发现任何具体的方法。 This suggests we shouldn't do it - The code within the method 'InitializeComponent' is generated by the designer and should not be manually modified 这表明我们不应该这样做- 方法“ InitializeComponent”中的代码是由设计人员生成的,不应手动修改
In the past, I had always used the form designer and never needed to manually make changes to InitializeComponent(). 过去,我一直使用表单设计器,而无需手动对InitializeComponent()进行更改。 However at my new firm, the tech lead has completely ignored the form designer and manually added most UI elements within InitializeComponent (ie it has either been heavily edited, or completely re-written to the extent that the Designer can't recreate the GUI in VS, but the GUI is visible and fully functional at when the code is executed) 但是在我的新公司中,技术主管完全忽略了表单设计器,并在InitializeComponent中手动添加了大多数UI元素(即,它已经过大量编辑,或者完全重写为设计器无法在其中创建GUI的程度)。 VS,但是在执行代码时,GUI是可见的并且功能齐全)
What is the advantage of not using the form designer? 不使用表单设计器的好处是什么? Is it just a matter of preference? 这只是偏好问题吗? Is manually editing/rewriting InitializeComponent for simple UI elements a good practice? 手动编辑/重写用于简单UI元素的InitializeComponent是一种好的做法吗?
Thanks! 谢谢!
There are only a few reasons why one might need to edit InitializeComponent, such as: 可能需要编辑InitializeComponent的原因只有几个,例如:
After editing InitializeComponent, I always save & switch back to Designer mode to make sure I didn't break anything. 编辑InitializeComponent之后,我总是保存并切换回Designer模式,以确保我没有破坏任何东西。
Any manual initialization code should be added outside InitializeComponent, eg OnLoaded() in a WinForms form or user control. 任何手动初始化代码都应添加到InitializeComponent外部,例如WinForms窗体或用户控件中的OnLoaded()。 As for fixing existing forms, it may be simple or nearly impossible depending on how complicated the form is, especially if controls have been added manually and required initialization methods (such as SuspendLayout, BeginInit) aren't being called. 至于修复现有表单,取决于表单的复杂程度,它可能是简单的或几乎是不可能的,尤其是如果已手动添加控件且未调用所需的初始化方法(例如SuspendLayout,BeginInit)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.