我有一個名爲Editor
的用戶控件,它是StackCustomWindow
命名空間的一部分。命名空間StackCustomWindow
包含該程序的主窗體。當我使用設計,Visual Studio 2010的地方像這樣的代碼在設計中添加Editor
用戶控件到主窗口:爲什麼在將Visual Studio添加到設計器時,Visual Studio會將名稱空間附加到用戶控件的名稱上?
this.editor1 = new StackCustomWindow.Editor();
時,它應該僅僅是這樣的:
this.editor1 = new Editor();
編譯拋出例外:
錯誤1類型名稱「編輯器」中不存在類型
'StackCustomWindow.StackCustomWindow' C:\用戶\裏卡多\文件\ Visual Studio中
2010 \項目\ 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
}
}
有一個[編輯]屬性(全名EditorAttribute) - 我不認爲這可能會導致問題嗎? – 2012-08-06 14:57:08
[Windows窗體設計器的可能的重複 - 在類的前面自動添加名稱空間](http://stackoverflow.com/questions/7300292/windows-forms-designer-automatically-adds-namespace-in-front-of-class) – meJustAndrew 2016-10-19 15:55:46