是否可以在 Designer 生成的代码中重新排列布局调整语句?

Can layout-adjusting statements be re-arranged in code generated by Designer?

为了方便合并*.Designer.vb个文件中的改动(*.Designer.cs个文件也是如此),我需要对某些代码段的内容进行排序在 Designer 生成的文件中,因为 Visual Studio is sometimes reordering them randomly。我可以在重新排列之前看到 16 个合并冲突,之后 个冲突。

我对排序声明等毫无疑问。但我的疑问是 ResumeLayout(false)PerformLayout()InitializeComponent() 方法末尾的调用。尽管学习了一些知识,但我还没有完全理解它们是如何工作的(practical details found here),因此我正在寻求一个答案是否可以安全地重新安排顺序他们被召唤的对象。到目前为止,使用 Designer 进行的测试没有显示出问题,但我想确定一下。

原始顺序 Form1.Designer.vb

Private Sub InitializeComponent()

    '...initializations of controls are left out

    '
    'Form1
    '
    Me.ClientSize = New System.Drawing.Size(784, 562)
    Me.Controls.Add(Me.TableLayoutPanel1)
    Me.Icon = CType(resources.GetObject("$this.Icon"), System.Drawing.Icon)
    Me.Name = "Form1"
    Me.Text = "Form1 Title"

    'original order created by Designer
    CType(Me.ErrorProvider1, System.ComponentModel.ISupportInitialize).EndInit()
    Me.TableLayoutPanel1.ResumeLayout(False)
    Me.TableLayoutPanel1.PerformLayout()
    Me.page2.ResumeLayout(False)
    Me.page2.PerformLayout()
    Me.page1.ResumeLayout(False)
    Me.TableLayoutPanel2.ResumeLayout(False)
    Me.TableLayoutPanel2.PerformLayout()
    Me.TabCtl1.ResumeLayout(False)
    CType(Me.PictureBox1, System.ComponentModel.ISupportInitialize).EndInit()
    'end of section in question

    Me.ResumeLayout(False)

End Sub

已修改 Form1.Designer.vb

中的顺序
Private Sub InitializeComponent()

    '...initializations of controls are left out

    '
    'Form1
    '
    Me.ClientSize = New System.Drawing.Size(784, 562)
    Me.Controls.Add(Me.TableLayoutPanel1)
    Me.Icon = CType(resources.GetObject("$this.Icon"), System.Drawing.Icon)
    Me.Name = "Form1"
    Me.Text = "Form1 Title"

    'the following lines (or pairs) were arranged alphabetically - is it OK?
    CType(Me.ErrorProvider1, System.ComponentModel.ISupportInitialize).EndInit()
    CType(Me.PictureBox1, System.ComponentModel.ISupportInitialize).EndInit()
    Me.page1.ResumeLayout(False)
    Me.page2.ResumeLayout(False)
    Me.page2.PerformLayout()
    Me.TabCtl1.ResumeLayout(False)
    Me.TableLayoutPanel1.ResumeLayout(False)
    Me.TableLayoutPanel1.PerformLayout()
    Me.TableLayoutPanel2.ResumeLayout(False)
    Me.TableLayoutPanel2.PerformLayout()
    'end of sorted groups

    Me.ResumeLayout(False)

End Sub

这样的改变总是安全的吗?或者命令应该根据控件的树层次结构(首先列出父级)而不是字母顺序排列?

编辑:一些进一步的研究表明,表单设计器将祖先放在首位,但不强制执行其他特定顺序。控件的顺序仅由 Control.Add() 调用的顺序决定。也许我可以改进排序方法来反映这一点,我应该是安全的。

我自己的回答是也许不是。在我最新的解决方案中,我避免依赖于知道确切答案。

我通过 将对象作为树排序 (将父对象放在第一位 + 在每个节点内按字母顺序排序)而不是按字母顺序倾倒对象来解决这个问题。这保留了 Visual Studio 设计器如何对对象进行排序的逻辑,同时还通过应用始终确定的顺序来修复 Visual Studio "randomness"。

*.Designer.vb 的重新排列内容的工具工作正常。如果有人感兴趣,我可能会把它作为开源发布。