编辑 InitializeComponent() 方法 C# [英] Editing the InitializeComponent() method C#

查看:35
本文介绍了编辑 InitializeComponent() 方法 C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经浏览了多个资源,试图找到何时手动将代码添加到 InitializeComponent 的用例,但没有找到任何具体的东西.这表明我们不应该这样做 - InitializeComponent"方法中的代码由设计者生成,不应手动修改

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. 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().然而,在我的新公司,技术负责人完全忽略了表单设计器,并在 InitializeComponent 中手动添加了大部分 UI 元素(即它要么被大量编辑,要么完全重写到设计器无法在其中重新创建 GUI 的程度.VS,但在执行代码时 GUI 是可见的并且功能齐全)

In the past, I had always used the form designer and never needed to manually make changes to InitializeComponent(). However at my new firm, the tech lead has completely ignored the form designer and manually added most UI elements within InitializeComponent (i.e. 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)

不使用表单设计器有什么好处?这只是喜好问题吗?为简单的 UI 元素手动编辑/重写 InitializeComponent 是一个好习惯吗?

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?

谢谢!

推荐答案

需要编辑 InitializeComponent 的原因只有几个,例如:

There are only a few reasons why one might need to edit InitializeComponent, such as:

  1. 纠正由糟糕的工具箱项设计者引入的错误.
  2. 删除无法从 VS 设计器中删除的控件的所有痕迹.(这必须小心完成.)
  3. 进行快速的设计者友好"调整:更改属性值、添加属性设置或删除属性设置.添加设置时,我确保它看起来就像设计者生成的代码.

编辑InitializeComponent后,我总是保存&切换回设计器模式以确保我没有破坏任何东西.

After editing InitializeComponent, I always save & switch back to Designer mode to make sure I didn't break anything.

任何手动初始化代码都应该添加到 InitializeComponent 之外,例如WinForms 窗体或用户控件中的 OnLoaded().至于修复现有表单,可能很简单,也可能几乎不可能,具体取决于表单的复杂程度,尤其是在手动添加控件且未调用所需的初始化方法(如 SuspendLayout、BeginInit)的情况下.

Any manual initialization code should be added outside InitializeComponent, e.g. OnLoaded() in a WinForms form or user control. 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.

这篇关于编辑 InitializeComponent() 方法 C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆