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

时间:2021-07-05 03:41:57

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

4 个解决方案

#1


3  

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()等。

#2


1  

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.

但是,当您在设计器中对自定义控件执行任何操作时,将在设计器文件中生成非常重要且有用的代码。比如创建任何添加的控件,事件处理程序连接以及基本的初始化和布局。此代码无法删除。

#3


1  

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)那么只有它会受到影响。

#4


0  

Strictly speaking I don't think so, but then you will lose any design time benefit you would get when creating the control, and my guess is that VS will keep trying to recreate them.

严格来说,我不这么认为,但是在创建控件时你将失去任何设计时间的好处,我的猜测是VS将继续尝试重新创建它们。

#1


3  

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()等。

#2


1  

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.

但是,当您在设计器中对自定义控件执行任何操作时,将在设计器文件中生成非常重要且有用的代码。比如创建任何添加的控件,事件处理程序连接以及基本的初始化和布局。此代码无法删除。

#3


1  

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)那么只有它会受到影响。

#4


0  

Strictly speaking I don't think so, but then you will lose any design time benefit you would get when creating the control, and my guess is that VS will keep trying to recreate them.

严格来说,我不这么认为,但是在创建控件时你将失去任何设计时间的好处,我的猜测是VS将继续尝试重新创建它们。