C#: ResumeLayout(true)是否与ResumeLayout(false) + PerformLayout()相同?

17

我已经查看了生成的FormUserControl的设计器代码,在InitializeComponent()方法中它们总是以以下内容开头:


I have looked at the generated designer code of Forms and UserControls, and in the InitializeComponent() method they always start with
    this.SuspendLayout();

以...结束

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

但从我在MSDN文档中看到的这些方法,似乎以此结尾不太对吧。

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

要做完全相同的事情吗?还是我漏掉了什么?

我问这个问题是因为我将在不同的方法中添加大量控件,而认为我应该执行挂起-恢复程序以使其更加高效。但我无法弄清楚为什么需要这两个方法调用,当你似乎只需要使用一个方法调用...

2个回答

8

使用反射器:

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

但是

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

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


那么为了获得正确的行为,我猜我应该同时调用它们两个? - Svish
很遗憾,看起来是这样的。 - SwDevMan81
1
好的。那么,谢谢你提供的信息 :) 我必须说我不太理解 ResumeLayout(true) 的意义,但它可能有其原因。 - Svish
我想请求有人自愿提供一个常见或典型的场景,其中“true”参数将产生与“false”参数不同的行为或结果。谢谢。 - FloverOwe

4

SuspendLayout

当向父控件添加多个控件时,建议在初始化要添加的控件之前调用 SuspendLayout 方法。在将控件添加到父控件后,请调用 ResumeLayout 方法。这将提高具有许多控件的应用程序的性能。

PerformLayout

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

MSDN 链接 - PerformLayout 方法


这很有道理,但是ResumeLayout呢?调用ResumeLayout(true)是否执行与调用ResumeLayout(false)PerformLayout()相同的工作? - Svish
2
我认为这个链接会回答你的问题 - http://www.ddj.com/architect/184405892 - KV Prajapati

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接