简体   繁体   English

编辑InitializeComponent()方法C#

[英]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的原因只有几个,例如:

  1. To correct bugs introduced by a bad toolbox item designer. 纠正由不良的工具箱项目设计者引入的错误。
  2. To remove all traces of a control that cannot be deleted from the VS designer. 删除无法从VS设计器中删除的控件的所有痕迹。 (This has to be done carefully.) (必须仔细进行此操作。)
  3. To make quick "designer friendly" tweaks: changing a property value, adding a property setting, or removing a property setting. 要快速进行“对设计者友好”的调整,请执行以下操作:更改属性值,添加属性设置或删除属性设置。 When adding a setting, I make sure it looks just like the designer-generated code. 添加设置时,我确保它看起来就像设计师生成的代码一样。

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.

相关问题 C#中的dispose()和initializeComponent()方法产生问题 - dispose() and initializeComponent() method in C# make problems C# - InitializeComponent问题 - C# - Issue with InitializeComponent C#覆盖基本表单文本-InitializeComponent - C# override base form text - InitializeComponent C#“无法解析方法'InitializeComponent'。解析器报告以下错误'无效符号种类:NamedType'” - C# "Failed to parse method 'InitializeComponent'. The parser reported the following error 'Invalid symbol kind: NamedType'" C# Windows 应用程序 - InitializeComponent() 上的用户设置 - C# Windows Application - User Settings on InitializeComponent() 在 WPF 和 C# 中构建 MainWindow() 和 InitializeComponent() - MainWindow() and InitializeComponent() building in WPF and C# C#中的InitializeComponent不存在 - InitializeComponent in C# doesn't exsist InitializeComponent()中的C#'System.StackOverflowException' - C# 'System.StackOverflowException' in InitializeComponent() 当前上下文中不存在名称“InitializeComponent”(c#,xamarin) - The name 'InitializeComponent' does not exist in the current context(c#,xamarin) 如何在C#控件中获取InitializeComponent以便在运行时实例化派生控件 - How to get InitializeComponent to instantiate a derived control at runtime in a C# control
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM