C# 类型名称 {myUserControl} 在类型 {myNamespace.myNamespace} 中不存在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1525595/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The type name {myUserControl} does not exist in the type {myNamespace.myNamespace}
提问by DRapp
I have a problem (obviously the question :)
我有一个问题(显然是问题:)
I have a project-- MyProject... hence the rest of the project uses a default of any classes as namespace "MyProject"... no problem.
我有一个项目——MyProject...因此项目的其余部分使用任何类的默认名称作为命名空间“MyProject”...没问题。
In my project, I created a custom user control that has many other controls on it (label, textboxes, etc). So, that class is ALSO within the default namespace of "MyProject". All compiles no problem. Just to confirm scope visibility, on this user control, I made sure that the DESIGNER code and the Code-Behind (My code) are BOTH within the same "MyProject" namespace (they are), AND they are both respectively PUBLIC PARTIAL CLASS MyUserControl.
在我的项目中,我创建了一个自定义用户控件,上面有许多其他控件(标签、文本框等)。因此,该类也在“MyProject”的默认命名空间中。全部编译没问题。只是为了确认范围可见性,在这个用户控件上,我确保设计者代码和代码隐藏(我的代码)都在同一个“MyProject”命名空间内(它们是),并且它们分别是 PUBLIC PARTIAL CLASS MyUserControl .
Now the issue. I create a simple form (also in namespace "MyProject" by default). From the toolbox, the "MyUserControl" exists so I drag it onto MyNewForm. Drag/Drop is fine.
现在的问题。我创建了一个简单的表单(默认情况下也在命名空间“MyProject”中)。从工具箱中,存在“MyUserControl”,因此我将其拖到 MyNewForm 上。拖放很好。
Save all, compile, fail... The Designer is adding an extra "MyProject" reference thus making it appear that the user control is actually located at MyProject.MyProject.MyUserControl .. instead of MyProject.MyUserControl.
全部保存、编译、失败...设计器添加了一个额外的“MyProject”引用,从而使用户控件看起来实际上位于 MyProject.MyProject.MyUserControl .. 而不是 MyProject.MyUserControl。
As soon as I manually remove the extra "MyProject.", save and compile, all is fine. However, if I re-edit the form, change something, M$ changes it back to the original "MyProject.MyUserControl" reference.
一旦我手动删除额外的“MyProject。”,保存并编译,一切都很好。但是,如果我重新编辑表单,更改某些内容,M$ 会将其更改回原始的“MyProject.MyUserControl”引用。
All that being said, here are the snippets from my project...
话虽如此,这里是我项目的片段......
namespace MyProject
{
partial class MyNewForm
{
...
private void InitializeComponent()
{
// THIS is the line that has the extra "MyProject." reference
// when I manually remove it, all works perfectly
this.MyUserControl1 = new MyProject.MyUserControl();
}
}
private MyUserControl MyUserControl1;
}
Then, in the MyUserControl definition I have...
然后,在 MyUserControl 定义中,我有...
namespace MyProject
{
public partial class MyUserControl : UserControl
...
}
and from the MyUserControl via the Designer...
并从 MyUserControl 通过设计器...
namespace MyProject
{
public partial class MyUserControl : UserControl
...
}
Thanks for the help...
谢谢您的帮助...
采纳答案by manji
What the designer is doing is ok.
设计师的做法没问题。
--> You have somehere in your project a namespace called MyProject.MyProject
.
--> 您的项目中有一个名为MyProject.MyProject
.
(Try finding it in "Class View")
(尝试在“类视图”中找到它)
回答by WhoIsRich
Since this was a top search result when I had this error, just want to post my cause and solution.
由于这是我遇到此错误时的热门搜索结果,因此只想发布我的原因和解决方案。
- I had two projects within a solution, sharing a 'common' class file which was added as a link.
- I added a second 'helper' class file as a link, used its code within the first, and got the error.
- 我在一个解决方案中有两个项目,共享一个作为链接添加的“通用”类文件。
- 我添加了第二个“helper”类文件作为链接,在第一个中使用了它的代码,但得到了错误。
The problem was I had not added the second 'helper' class as a link in both projects.
问题是我没有在两个项目中添加第二个“助手”类作为链接。
So the other project had an updated 'common' class, but no knowledge of the 'helper' class it now used.
所以另一个项目有一个更新的“common”类,但不知道它现在使用的“helper”类。
Note to self: pay more attention to the project column of the error list :)
自我注意:多注意错误列表的项目栏:)
回答by Muzzy B.
PS. to anyone who has same problem but has not found any solution...
附注。对于任何有同样问题但没有找到任何解决方案的人......
Assuming you have created a new WindowsFormApplication;
假设您已经创建了一个新的 WindowsFormApplication;
- Create a new WindowsFormApplication project using same name as its solution name.
- The default pre-created Form name comes called "Form1". And change its name same as the project name.
- Add new UserControl class into the project.
- Build/Rebuild the project and check the usercontrol is located at Toolbox.
- Drag the usercontrol onto the form and start debugging.
- 使用与其解决方案名称相同的名称创建一个新的 WindowsFormApplication 项目。
- 默认的预创建表单名称称为“Form1”。并将其名称更改为与项目名称相同。
- 将新的 UserControl 类添加到项目中。
- 构建/重建项目并检查用户控件位于工具箱。
- 将用户控件拖到窗体上并开始调试。
- Error: The type name 'userControlName' does not exist in the type 'projectName.FormName'
- 错误:类型名称“userControlName”在类型“projectName.FormName”中不存在
I had research on net for any solution but couldn't come up with any answer...
我在网络上研究过任何解决方案,但无法想出任何答案......
But if you change the form name any other different from the project name, it'll be resolved.
但是,如果您将表单名称更改为与项目名称不同的任何其他名称,它将被解析。
If you insist on that the form name and project name has to be same depending on your project needs, a custom DLL could be created and use the usercontrol in it.
如果您坚持根据您的项目需要,表单名称和项目名称必须相同,则可以创建自定义 DLL 并在其中使用用户控件。
Then to use as a control, add the DLL file to "ToolBox" using "Choose Items..."
然后用作控件,使用“选择项目...”将 DLL 文件添加到“工具箱”
Finally it is going to be ready to use.
终于可以使用了。
PS2. struggling the same problem for hours, this is the solution I found.
PS2。几个小时都在为同样的问题苦苦挣扎,这是我找到的解决方案。
回答by AndroidLearner
Name of User Control and Form are same. Using different names will solve the issue.
用户控件和表单的名称相同。使用不同的名称将解决问题。
回答by Mike
The Namespace Name and Class Name need to be different. The code generated by adding the WCF automatically references the Namespace but if the Class name is the same as the Namespace name, the generated code looks at the Class and nothing will compile.
Namespace Name 和 Class Name 需要不同。添加 WCF 生成的代码会自动引用 Namespace,但如果 Class 名称与 Namespace 名称相同,则生成的代码会查看 Class,并且不会编译任何内容。
回答by Reid Evans
Just encountered this where I had a MasterPage that had an explicit
刚遇到这个,我有一个 MasterPage,它有一个明确的
<%@ Import Namespace="MyNamespace" %>
in the .master file
在 .master 文件中
回答by Hammad Khan
This also happens when you use different pages but with same name. In my case I had created "Grants.xsd" dataset and "Grants.aspx" page. Somehow they got in conflict resulting in this error.
当您使用不同的页面但名称相同时,也会发生这种情况。就我而言,我创建了“Grants.xsd”数据集和“Grants.aspx”页面。不知何故,他们发生了冲突,导致了这个错误。
You can easily trouble shoot this by hovering over the culprit keyword (class name) and in Visual Studio 2013, it will tell you exactly where the conflict is.
您可以通过将鼠标悬停在罪魁祸首关键字(类名)上轻松解决此问题,并且在 Visual Studio 2013 中,它会告诉您冲突的确切位置。