【问题标题】:C#: Does ResumeLayout(true) do the same as ResumeLayout(false) + PerformLayout()?C#:ResumeLayout(true) 是否与 ResumeLayout(false) + PerformLayout() 一样?
【发布时间】:2010-11-23 07:26:14
【问题描述】:

我查看了Forms 和UserControls 生成的设计器代码,在InitializeComponent() 方法中它们总是以

    this.SuspendLayout();

并以

结尾
    this.ResumeLayout(false);
    this.PerformLayout();

但是从我在这些方法的 msdn 文档中可以看到,不会以

结尾
    this.ResumeLayout(true); // Or just this.ResumeLayout()

做同样的事情?还是我在这里遗漏了什么?

问是因为我将以不同的方法添加一堆控件,并认为我应该执行挂起-恢复例程以更好和高效。但是当您似乎只能使用一个时,无法弄清楚这两个方法调用的原因是什么......

【问题讨论】:

    标签: c# winforms windows-forms-designer generated-code


    【解决方案1】:

    SuspendLayout

    将多个控件添加到一个 父控件,建议 你调用 SuspendLayout 方法 在初始化控件之前 添加。添加控件后 父控件,调用 简历布局方法。这会 提高性能 具有许多控件的应用程序。

    执行布局

    它强制控件应用布局 其所有子控件的逻辑。如果 SuspendLayout 方法是 在调用之前调用 PerformLayout 方法,Layout 事件被抑制。 layout 事件可以使用 SuspendLayout 和 ResumeLayout 方法。

    MSDN 链接 - PerformLayout Method

    【讨论】:

    • 有道理,但是ResumeLayout 呢?调用 ResumeLayout(true) 是否与调用 ResumeLayout(false)PerformLayout() 执行相同的工作?
    • 我认为这个链接会回答你的问题 - ddj.com/architect/184405892
    【解决方案2】:

    使用反射器:

    this.ResumeLayout() is equal to this.ResumeLayout(true)
    

    但是

    this.ResumeLayout(true) is not equal to this.ResumeLayout(false) + this.PerformLayout()
    

    原因:
    当 ResumeLayout 被调用为 false 时,会有一个控件集合被循环遍历,并且 LayoutEngine 会在布局中的每个控件上调用 InitLayout。

    【讨论】:

    • 所以要获得正确的行为,我想我必须同时调用它们?
    • 不幸的是它看起来那样
    • 好的。那么,谢谢你的信息:) 我必须说我不太明白ResumeLayout(true) 的意思,但它可能有它的原因。
    猜你喜欢
    • 2010-10-24
    • 1970-01-01
    • 2012-08-16
    • 2010-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多