首页 > 解决方案 > 运行应用程序时 Visual Studio 不显示 UI

问题描述

我已经在 c# 上工作了大约 3 个小时,并且 Visual Studio 在运行应用程序时不显示 UI。它在设计器中显示 UI,但在编译和运行后,它只是空白。

在此处输入图像描述

标签: c#visual-studio

解决方案


要测试的两件事:

  1. 在代码文件Program.cs中,您会发现如下内容:

    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }
    

    该行Application.Run中的表单名称是否与您的表单名称匹配?

  2. 您的表单代码(Form1.cs)应该有一个如下所示的构造函数

    public Form1() // Where the name of the constructor must match the one of the form class.
    {
        InitializeComponent();
        // Your code goes here (if any) ...
    }
    

    它有这个构造函数吗?如果是,它会调用InitializeComponent吗?

InitializeComponent非常重要,因为它创建控件并配置表单。您可能已将其替换为您自己的代码。始终在初始化代码之前调用它。


推荐阅读