2012-08-06 78 views
2

我有一個名爲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 

    } 
} 
+0

有一個[編輯]屬性(全名EditorAttribute) - 我不認爲這可能會導致問題嗎? – 2012-08-06 14:57:08

+0

[Windows窗體設計器的可能的重複 - 在類的前面自動添加名稱空間](http://stackoverflow.com/questions/7300292/windows-forms-designer-automatically-adds-namespace-in-front-of-class) – meJustAndrew 2016-10-19 15:55:46

回答

9

問題是StackCustomWindow類型與StackCustomWindow命名空間的命名方式相同。因此,StackCustomWindow.Editor最終被解釋爲「StackCustomWindow類型」的「成員」編輯器「,而不是」名稱空間StackCustomWindow的「編輯器」「。

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

你的代碼是有效的,設計者生成的代碼不是。代碼生成器並不期望你做你做的事情,並且處理不好。

+1

有趣的是,Visual Studio並沒有提醒你這個問題,但也許是因爲它是一個業餘的錯誤,或者這樣的警告可能會使問題過於複雜(是的,這可以解決問題)。 – 2012-08-06 15:04:09

+0

@svinja你釘了它!如果你有例如一個項目的模塊聲明使用相同的項目名稱,那麼VB.NET也會發生同樣的情況。像'AppGlobals.AppGlobals.vb'一樣。 VB用戶(不習慣看名字空間聲明):在Project Properties的第一個選項卡中更改這個名稱空間。 – 2015-09-21 17:08:40

-1

您可以與System.Collections.Stack System.Collections.Generic.Stack類中發生衝突。你正在使用這些命名空間(System.Collections,System.Collections.Generic)嗎?

+0

不,我不包括那些。不過,我不知道爲什麼這會與編輯發生衝突。如果我將'Stack'重命名爲'StackCustomWindow',我仍然遇到問題(我更新了問題以反映名稱更改)。 – 2012-08-06 14:48:05

相關問題