简体   繁体   English

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

[英]Why does Visual Studio append the namespace to the name of a user control when adding it to the designer?

I have a user control named Editor that is part of the StackCustomWindow namespace. 我有一个名为Editor的用户控件,它是StackCustomWindow命名空间的一部分。 The StackCustomWindow namespace contains the main form for the program. StackCustomWindow命名空间包含程序的主窗体。 When I add the Editor user control to the main window using the designer, Visual Studio 2010 places code like this in the designer: 当我使用设计EditorEditor用户控件添加到主窗口时,Visual Studio 2010会在设计器中放置这样的代码:

this.editor1 = new StackCustomWindow.Editor();

when it should just be this: 什么时候应该这样:

this.editor1 = new Editor();

Compiling throws an exception: 编译抛出异常:

Error 1 The type name 'Editor' does not exist in the type 错误1类型中不存在类型名称“编辑器”

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

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

I found a similar question , with a solution that said a duplicate name must exist somewhere in the solution, but a) I don't know why that would exist in this case, since I have no other controls with that name, and b) I don't know how to check if a duplicate name actually does exist. 我找到了一个类似的问题 ,解决方案说解决方案中某处必须存在重复的名称,但是a)我不知道为什么在这种情况下会存在,因为我没有其他具有该名称的控件,并且b)我不知道如何检查是否确实存在重复的名称。 I don't have any other user controls or items named Editor , and Visual Studio does this for all of my user controls. 我没有任何其他用户控件或名为Editor项目,Visual Studio为我的所有用户控件执行此操作。 As you can see below, all of the code is very basic, with no controls besides the user control and the main window. 如下所示,所有代码都是非常基本的,除了用户控件和主窗口之外没有任何控件。

Code for Editor : Editor代码:

using System.Windows.Forms; 使用System.Windows.Forms;

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

and it's designer.cs file: 它是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

    }
}

StackCustomWindow.cs: StackCustomWindow.cs:

using System.Windows.Forms; 使用System.Windows.Forms;

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

    }
}

StackCustomWindow.designer.cs: 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

    }
}

The problem is that the StackCustomWindow Type is named the same as the StackCustomWindow namespace. 问题是StackCustomWindow Type的名称与StackCustomWindow名称空间的名称相同。 So StackCustomWindow.Editor ends up being interpreted as "member 'Editor' of type StackCustomWindow", instead of "member 'Editor' of namespace StackCustomWindow". 所以StackCustomWindow.Editor最终被解释为StackCustomWindow类型的“成员'编辑器”,而不是命名空间StackCustomWindow的“成员'编辑器”。

Using a different name for the namespace and the type will prevent this problem. 对命名空间和类型使用不同的名称将防止此问题。

Your code is valid, the code generated by the designer isn't. 您的代码有效,设计人员生成的代码不是。 The code generator doesn't expect you to do what you did and doesn't handle it well. 代码生成器不希望你做你做的事情,也不能很好地处理它。

You may be clashing with the System.Collections.Stack System.Collections.Generic.Stack class. 您可能会与System.Collections.Stack System.Collections.Generic.Stack类发生冲突。 Are you using either of these namespaces ( System.Collections , System.Collections.Generic )? 您使用这些命名空间中的任何一个( System.CollectionsSystem.Collections.Generic )?

暂无
暂无

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

相关问题 将Visual Studio 2008添加到Telerik RadGrid EditFormTemplate中时,Visual Studio 2008不会将控件添加到设计器文件中 - Visual Studio 2008 does not add control to designer file when adding it into a Telerik RadGrid EditFormTemplate 为什么Visual Studio Designer会删除代码? - Why does the Visual Studio Designer delete code? 在设计器中打开自定义用户控件时,Visual Studio Professional 15.9.2崩溃 - Visual Studio Professional 15.9.2 crashes when opening a custom user control in the designer 为什么我的用户控件会导致 Visual Studio 崩溃? - Why does my user control crash Visual Studio? (在设计器中)选定的Visual Studio复制控件名称以粘贴到文本编辑器中 - visual studio copy selected (in designer)control name to paste into text editor 标记以在Visual Studio Designer中编辑用户控件的DataMember - Tag to edit DataMember of user control in visual studio designer designer.cs在Visual Studio中使用用户控件的问题 - designer.cs issues with using user control in Visual Studio 为什么某些控件属性在Visual Studio设计器中显示,而其他控件属性没有? - Why are certain control properties shown in the Visual Studio designer, and others not? 当用户控件在公共属性上具有Browsable false时,为什么设计器在添加到表单时将其设置为null? - When a user control has Browsable false on public property, why does designer set it to null when added to a form? Visual Studio Designer中的别名和名称空间冲突 - Alias and namespace conflict in Visual Studio Designer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM