Control.SuspendLayout和Control.ResumeLayout是否保持计数?

Sim*_*mon 4 .net c# layout winforms

我无法想到如何准确地说出这个问题,但希望我的意思会很明确.Control.SuspendLayout和Control.ResumeLayout是否保持计数?

换句话说,如果我两次调用SuspendLayout,并且ResumeLayout调用一次,布局是否仍然被暂停?

Han*_*ant 8

没有什么理由可以陷入这样的问题.源代码可用,标题为"参考源".获得它的最佳方法是使用.NET Mass Downloader.并非每个.NET程序集都公布了其源代码,您的备份是值得尊敬的Reflector.

Anyhoo,源代码看起来大致如下:

private byte layoutSuspendCount;

public void SuspendLayout() {
  layoutSuspendCount++;
  if (layoutSuspendCount == 1) OnLayoutSuspended();
}

public void ResumeLayout() {
  ResumeLayout(true);
}

public void ResumeLayout(bool performLayout) {
  if (layoutSuspendCount > 0) {
    if (layoutSuspendCount == 1) OnLayoutResuming(performLayout);
    layoutSuspendCount--;
    if (layoutSuspendCount == 0 && performLayout) {
      PerformLayout();
    }
  }
} 

internal void PerformLayout(LayoutEventArgs args) {
  if (layoutSuspendCount > 0) {
    //...
    return;
  }
  //etc...
}
Run Code Online (Sandbox Code Playgroud)

所以你的问题的答案是:是的.