C# 什么';容器类等的含义是什么?

C# 什么';容器类等的含义是什么?,c#,winforms,C#,Winforms,创建新的Windows窗体时,Visual Studio会自动为我提供两个文件: MyForm.cs using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.

创建新的Windows窗体时,Visual Studio会自动为我提供两个文件:

MyForm.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Hardest_Game
{
    public partial class MyForm : Form
    {
        public MyForm()
        {
            InitializeComponent();
        }
    }
}
MyForm.Designer.cs

namespace Hardest_Game
{
    partial class MyForm
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Text = "MyForm";
        }

        #endregion
    }
}
它看起来更干净,使用更少的东西,等等。为什么我要使用微软希望我使用的方法


编辑:好吧,对不起,伙计们,我的第二个问题可能不是最好的,让我再试一次:如果我以编程方式创建一个程序(就像我在第三段代码中所做的那样),包括它的所有UI等,使用
IContainer
Dispose()
,我会受益吗?这就是为什么我问它们的用途是什么,想知道我是否应该自己使用它们,或者它们是否只用于自动生成的代码。

当你得到一个.Designer.cs文件时,它是一个以某种方式自动生成的文件。修改它(几乎总是)将导致在从文件源重新生成文件时覆盖所做的更改。在这种情况下,当您使用拖放工具更改UI时,将生成并重新生成设计器文件

这就是存在两个文件的原因:您有一个分部类,该类中有一个部分隐藏在一个文件中,您不能也不应该修改;另一个部分可以也应该修改,但不会被其他文件中的设计器生成器触及。编译代码时,两个文件中的类定义会“合并”到一起。如果没有
partial
关键字,您将无法做到这一点——类无法跨越文件边界


这基本上也回答了第二个问题:不要担心Designer.cs文件中发生了什么

该类被分成两个文件(),这样您就可以将自动生成的设计器代码与自己的逻辑分开。VisualStudio中的WinForms设计器将在设计器文件中自动生成代码,这实际上会添加并定位所有控件。(因此不应手动修改)


容器
对象用于保存非UI组件,例如计时器。dispose方法会自动实现,以便在应用程序/窗口关闭时清理这些资源。请参见?

其中一个类包含您自己编写的代码,另一个类是从表单的可视化设计生成的。在设计器中,将控件(例如文本框)拖到窗体上时,VisualStudio会自动添加该控件以及一组默认属性


将两者分开,可以将表单的可视部分视为与代码分开。

设计器类是自动生成的。你不必碰那个文件。该文件中到处都有注释,如
不要使用代码编辑器修改此方法的内容。
。我不认为每次更改表单时都会删除您的唯一文件。因此,如果我不使用设计器,而是以编程方式创建UI,我的版本会正常工作吗?我是否仍应为所有非UI对象创建一个
容器
?我已经编辑了我的主要问题!:)所有这些都是自动生成的对象,如果您使用可视化设计器进行了更改,则不建议您更改所创建的任何内容。如果您是完全以编程方式创建表单,那么您不需要什么都有。只需创建表单,然后创建对象(文本框、按钮等)并将它们添加到表单的控件集合中。听起来您正在尝试第三种方法,即按照可视化设计器的方法将表单创建为单独的文件,但您自己要做。我很好奇;这就是你想要做的吗?如果是,为什么?作为学习练习?或者你想要一个功能性的效果?这只回答了第一个问题,而不是第二个问题,第二个问题更重要。我想知道我是否需要
Dispose()
IContainer
来做任何事情,请阅读我编辑的新问题:)“不要担心Designer.cs文件中发生了什么。”如果你希望人们这样做编程,而不知道他们的程序代码中发生了什么。。。嗯,是的。
using System.Windows.Forms;
using System.Drawing;

namespace Hardest_Game
{
    class MainWindow : Form
    {
        public MainWindow()
        {
            this.InitializeComponent();
        }

        // Controls
        Button myButton { set; get; }

        void InitializeComponent()
        {
            myButton = new Button();
            myButton.Text = "My Button";
            this.Controls.Add(myButton);
        }
    }
}