【问题标题】:Can layout-adjusting statements be re-arranged in code generated by Designer?可以在 Designer 生成的代码中重新排列布局调整语句吗?
【发布时间】:2015-03-27 10:48:37
【问题描述】:

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

我对排序声明等毫无疑问。但我怀疑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() 调用的顺序决定。也许我可以改进排序方法来反映这一点,我应该是安全的。

【问题讨论】:

    标签: .net vb.net winforms visual-studio designer


    【解决方案1】:

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

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

    *.Designer.vb 的内容重排工具运行良好。如果有兴趣,也许我会将它作为开源发布。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-30
      • 2012-11-27
      • 2013-04-23
      • 2017-12-27
      相关资源
      最近更新 更多