【问题标题】:Why does Visual Studio append the namespace to the name of a user control when adding it to the designer?为什么在将用户控件添加到设计器时,Visual Studio 会将命名空间附加到用户控件的名称中?
【发布时间】:2012-08-03 13:14:58
【问题描述】:

我有一个名为Editor 的用户控件,它是StackCustomWindow 命名空间的一部分。 StackCustomWindow 命名空间包含程序的主窗体。当我使用设计器将Editor 用户控件添加到主窗口时,Visual Studio 2010 会在设计器中放置如下代码:

this.editor1 = new StackCustomWindow.Editor();

什么时候应该是这样的:

this.editor1 = new Editor();

编译抛出异常:

错误 1 ​​类型中不存在类型名称“Editor”

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

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

我找到了一个similar question,它的解决方案说在解决方案中的某处必须存在重复的名称,但是 a) 我不知道为什么在这种情况下会存在,因为我没有其他具有该名称的控件, 和 b) 我不知道如何检查重复名称是否确实存在。我没有任何其他名为Editor 的用户控件或项目,Visual Studio 为我的所有用户控件执行此操作。如下图所示,所有代码都很基础,除了用户控件和主窗口外没有其他控件。

Editor 的代码:

使用 System.Windows.Forms;

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

它是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:

使用 System.Windows.Forms;

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

    }
}

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

    }
}

【问题讨论】:

标签: c# winforms visual-studio-2010 namespaces


【解决方案1】:

问题在于 StackCustomWindow 类型的名称与 StackCustomWindow 命名空间相同。所以 StackCustomWindow.Editor 最终被解释为“StackCustomWindow 类型的成员 'Editor'”,而不是“命名空间 StackCustomWindow 的成员 'Editor'”。

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

您的代码有效,设计器生成的代码无效。代码生成器不希望你做你做过的事情,也没有很好地处理它。

【讨论】:

  • 有趣的是,Visual Studio 并没有就此发出警告,但可能是因为这是一个业余错误,或者这样的警告可能会使问题过于复杂(是的,这解决了问题)。
  • @svinja 你成功了!在 VB .NET 中也会发生同样的情况,例如,如果您有一个项目,其中的模块声明使用相同的项目名称。喜欢AppGlobals.AppGlobals.vb。 VB 用户(不习惯查看命名空间声明):在项目属性的第一个选项卡中更改此命名空间。
  • 即使在 vs2017 中仍然遇到同样的问题,我没有更改已在多个地方使用的类的名称,而是更改了命名空间,因为我的项目要小得多
【解决方案2】:

您可能与System.Collections.Stack System.Collections.Generic.Stack 类发生冲突。您是否在使用这些命名空间中的任何一个(System.CollectionsSystem.Collections.Generic)?

【讨论】:

  • 不,我不包括其中任何一个。不过,我不知道为什么这会导致与 Editor 发生冲突。如果我将 Stack 重命名为 StackCustomWindow 我仍然有问题(我更新了问题以反映名称更改)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-02-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-23
  • 2011-05-01
相关资源
最近更新 更多