容器类的含义是什么?

时间:2015-04-28 00:03:45

标签: 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.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
    }
}

我的第一个问题是,为什么要创建两个文件?为什么不在一个文件中定义方法?

第二个问题,也是更重要的问题,为什么我们在这里使用System.ComponentModel.Icontainer,使用所有Dispose()方法等等? 他们实际做了什么,msdn.microsoft.com没有提供太多信息,它只是解释说它们曾用于包含内容。

此代码似乎运行正常:

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);
        }
    }
}

它看起来更干净,使用更少的东西等等。为什么我会使用Microsoft希望我使用的方法?

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

3 个答案:

答案 0 :(得分:3)

该类分为两个文件(partial classes),以便您可以将自动生成的设计器代码与您自己的逻辑分开。 Visual Studio中的WinForms设计器将自动生成设计器文件中的代码,该文件实际上会添加和定位所有控件。 (并且不应该因此而手动修改)

container对象用于保存非UI组件,例如计时器。当您的应用程序/窗口关闭时,dispose方法会自动实现以清理这些资源。请参阅What's the purpose of the components IContainer generated by the Winforms designer

答案 1 :(得分:2)

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

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

答案 2 :(得分:-3)

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

这就是有两个文件的原因:你有一个部分课程,给你一个课程的一部分你不能修改隐藏在一个文件中,一个你可以而且应该修改那些不被设计师生成器触及的文件。编译代码时,两个文件中的类定义是&#34;合并&#34;重新走到一起。如果没有partial关键字,您将无法做到这一点 - 类无法跳过文件边界。

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