簡體   English   中英

為什么Visual Studio在將名稱空間添加到設計器時會將其添加到用戶控件的名稱中?

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

我有一個名為Editor的用戶控件,它是StackCustomWindow命名空間的一部分。 StackCustomWindow命名空間包含程序的主窗體。 當我使用設計EditorEditor用戶控件添加到主窗口時,Visual Studio 2010會在設計器中放置這樣的代碼:

this.editor1 = new StackCustomWindow.Editor();

什么時候應該這樣:

this.editor1 = new Editor();

編譯拋出異常:

錯誤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();
        }
    }
}

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

    }
}

問題是StackCustomWindow Type的名稱與StackCustomWindow名稱空間的名稱相同。 所以StackCustomWindow.Editor最終被解釋為StackCustomWindow類型的“成員'編輯器”,而不是命名空間StackCustomWindow的“成員'編輯器”。

對命名空間和類型使用不同的名稱將防止此問題。

您的代碼有效,設計人員生成的代碼不是。 代碼生成器不希望你做你做的事情,也不能很好地處理它。

您可能會與System.Collections.Stack System.Collections.Generic.Stack類發生沖突。 您使用這些命名空間中的任何一個( System.CollectionsSystem.Collections.Generic )?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM