Skip to main content
 首页 » 编程设计

c#之如何正确初始化 Windows 窗体应用程序

2024年02月23日11kenshinobiy

我编写了这个示例程序 - 简化了一个非常复杂的应用程序。相同的二进制 [.exe] 在某些机器上启动时会抛出空指针异常。所以,我想知道如何正确构建 Windows Forms 表单。

在我们的应用程序中,Form1_SizeChanged 方法是 this.ResumeLayout(false) 方法的结果,它是 InitializeComponents() 中的最后一条语句.我不知道要模拟那个,所以我只是为这个测试程序自己改变了尺寸。

public partial class Form1 : Form 
{ 
    public class Logger { 
        public Logger() { } 
        public void log(string str) { 
            Console.WriteLine("logging - " + str); 
        } 
    } 
 
    Logger logger = null; 
 
    public Form1() 
    { 
        InitializeComponent(); 
        this.Size = new Size(200, 300); 
        MyInitialize(); 
    } 
 
    private void MyInitialize() { 
 
        // Just that it takes some time. 
        Console.WriteLine("MyInitialize -- Enter"); 
        for (int count = 0; count <5 ; count++){ 
            Console.WriteLine("Sleeping -- " + count); 
            Thread.Sleep(1000); 
        } 
        logger = new Logger(); 
    } 
 
    private void sleepingPill(int millisec) { 
        Thread.Sleep(millisec); 
    } 
 
    private void Form1_SizeChanged(object sender, EventArgs e) 
    { 
        logger.log("Form1_SizeChanged -- Enter"); 
    } 
} 

根据我的理解,除非 Form1 构造正确,否则不应调用 Form1_SizeChanged。谁能阐明 Windows 窗体事件架构在这种情况下是如何工作的?

 
Original Stack Trace: from our complex application 
System.NullReferenceException was unhandled 
  Message=Object reference not set to an instance of an object. 
  Source=ABCD 
  StackTrace: 
       at ABCD.Form1.AppendToLog(String s) 
       at ABCD.Form1.Form1_SizeChanged(Object sender, EventArgs e) 
       at System.Windows.Forms.Control.OnSizeChanged(EventArgs e) 
       at System.Windows.Forms.Control.UpdateBounds(Int32 x, Int32 y, Int32 width, Int32 height, Int32 clientWidth, Int32 clientHeight) 
       at System.Windows.Forms.Control.UpdateBounds(Int32 x, Int32 y, Int32 width, Int32 height) 
       at System.Windows.Forms.Control.SetBoundsCore(Int32 x, Int32 y, Int32 width, Int32 height, BoundsSpecified specified) 
       at System.Windows.Forms.Form.SetBoundsCore(Int32 x, Int32 y, Int32 width, Int32 height, BoundsSpecified specified) 
       at System.Windows.Forms.Control.ScaleControl(SizeF factor, BoundsSpecified specified) 
       at System.Windows.Forms.ScrollableControl.ScaleControl(SizeF factor, BoundsSpecified specified) 
       at System.Windows.Forms.Form.ScaleControl(SizeF factor, BoundsSpecified specified) 
       at System.Windows.Forms.Control.ScaleControl(SizeF includedFactor, SizeF excludedFactor, Control requestingControl) 
       at System.Windows.Forms.ContainerControl.Scale(SizeF includedFactor, SizeF excludedFactor, Control requestingControl) 
       at System.Windows.Forms.ContainerControl.PerformAutoScale(Boolean includedBounds, Boolean excludedBounds) 
       at System.Windows.Forms.ContainerControl.PerformNeededAutoScaleOnLayout() 
       at System.Windows.Forms.ContainerControl.OnLayoutResuming(Boolean performLayout) 
       at System.Windows.Forms.Control.ResumeLayout(Boolean performLayout) 
       at ABCD.Form1.InitializeComponent() 
       at ABCD.Form1..ctor() 
       at ABCD.Program.Main() 
  InnerException: 
 

来自堆栈跟踪的通知:从 InitializeComponents() 调用 Form1_sizeChanged .. 我认为这不应该发生。 Form1_sizeChanged 是 Form1 类的实例方法,不应在构造 Form1 之前调用。如果 .NET 环境要处理这个事件,它应该等到 Form1 被正确构造,不是吗?

请您参考如下方法:

在构造函数的第二行中,您设置了大小。这将在创建记录器之前调用 Form1_SizeChanged()。在调用 MyInitialize 之后移动 Size 的设置。