简体   繁体   English

是否需要自定义Winforms控件自动生成的设计器文件?

[英]Are custom Winforms control auto generated designer files needed?

Visual Studio creates code similar to this: Visual Studio创建类似于以下代码:

namespace MyCustomControls
{
    partial class MyCustomControl
    {
        /// <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 Component 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 ( )
        {
            components = new System.ComponentModel.Container ( );
        }

        #endregion
    }
}

The custom designer file is essential if you are using the designer surface to lay out child controls etc (pretty-much anything in the designer). 如果要使用设计器图面来布置子控件等(设计器中几乎所有内容) ,则定制设计器文件必不可少。 If you aren't doing then this (for example, you are using custom painting, or adding the child controls manually), then you can usually kill this file pretty safely. 如果您这样做(例如,您正在使用自定义绘画,或手动添加子控件),则通常可以非常安全地杀死此文件。

In some ways this makes it easier to implement Dispose() etc. 在某些方面,这使得实现Dispose()等更加容易。

No, the code which is generated from the initial spit of creating a new custom control is not needed. 不,不需要从创建新的自定义控件的初始操作生成的代码。 It doesn't do anything functional and can essentially be commented out of your application. 它没有任何功能,实际上可以从应用程序中注释掉。

However, the moment you do anything with your custom control in the designer, very important and useful code will be generated into the designer file. 但是,当您在设计器中使用自定义控件执行任何操作时,非常重要和有用的代码将生成到设计器文件中。 Such as the creation of any added controls, event handler hookup and basic initialization and layout. 例如创建任何添加的控件,事件处理程序连接以及基本的初始化和布局。 This code cannot be deleted. 此代码无法删除。

You get those files, the partial class setup and Design surface because you started with the CustomControl Template. 因为您从CustomControl模板开始,所以获得了这些文件,部分类设置和设计图面。

You can also start a Control by adding a 'New Class' and add only the stuff you really need. 您还可以通过添加“新类”来启动控件,并仅添加您真正需要的内容。 If you don't use the designer (and you usually don't for a CustomControl) then having it only gets in the way. 如果您不使用设计器(并且通常不使用CustomControl),那么只会阻碍设计器。

严格来说,我不这么认为,但是那样您将失去创建控件时将获得的任何设计时间收益,而我的猜测是VS将继续尝试重新创建它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM