为什么Visual Studio在将名称空间添加到设计器时会将其添加到用户控件的名称中?

Ric*_*ano 2 c# namespaces visual-studio-2010 winforms

我有一个名为EditorStackCustomWindow命名空间的用户控件.该StackCustomWindow命名空间包含该程序的主要形式.当我Editor使用设计器将用户控件添加到主窗口时,Visual Studio 2010将这样的代码放在设计器中:

this.editor1 = new StackCustomWindow.Editor();
Run Code Online (Sandbox Code Playgroud)

什么时候应该这样:

this.editor1 = new Editor();
Run Code Online (Sandbox Code Playgroud)

编译抛出异常:

错误1类型中不存在类型名称"编辑器"

'StackCustomWindow.StackCustomWindow'C:\ Users\ricardo\Documents\Visual Studio

2010\Projects\StackCustomWindow\StackCustomWindow\StackCustomWindow.Designer.cs 35 51 StackCustomWindow

我发现了一个类似的问题,解决方案说解决方案中某处必须存在重复的名称,但是a)我不知道为什么在这种情况下会存在,因为我没有其他具有该名称的控件,并且b)我不知道如何检查是否确实存在重复的名称.我没有任何其他用户控件或命名项Editor,Visual Studio 为我的所有用户控件执行此操作.如下所示,所有代码都是非常基本的,除了用户控件和主窗口之外没有任何控件.

代码Editor:

使用System.Windows.Forms;

namespace StackCustomWindow
{
    public partial class Editor : UserControl
    {
        public Editor()
        {
            InitializeComponent();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

它的designer.cs文件:

namespace StackCustomWindow
{
    partial class Editor
    {
        /// <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()
        {
            this.SuspendLayout();
            // 
            // Editor
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Name = "Editor";
            this.Size = new System.Drawing.Size(452, 276);
            this.ResumeLayout(false);

        }

        #endregion

    }
}
Run Code Online (Sandbox Code Playgroud)

StackCustomWindow.cs:

使用System.Windows.Forms;

namespace StackCustomWindow
{
    public partial class StackCustomWindow : Form
    {
        public StackCustomWindow()
        {
            InitializeComponent();
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

StackCustomWindow.designer.cs:

namespace StackCustomWindow
{
    partial class StackCustomWindow
    {
        /// <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.SuspendLayout();
            // 
            // StackCustomWindow
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(718, 535);
            this.Name = "StackCustomWindow";
            this.Text = "StackCustomWindow";
            this.ResumeLayout(false);

        }

        #endregion

    }
}
Run Code Online (Sandbox Code Playgroud)

svi*_*nja 9

问题是StackCustomWindow Type的名称与StackCustomWindow名称空间的名称相同.所以StackCustomWindow.Editor最终被解释为StackCustomWindow类型的"成员'编辑器",而不是命名空间StackCustomWindow的"成员'编辑器".

对命名空间和类型使用不同的名称将防止此问题.

您的代码有效,设计人员生成的代码不是.代码生成器不希望你做你做的事情,也不能很好地处理它.