C# 在 Windows 窗体设计器中加载窗体时出现“找不到类型”错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9314/
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
"Could not find type" error loading a form in the Windows Forms Designer
提问by Orion Edwards
I have a .NET 2.0 windows forms app, which makes heavy use of the ListView
control.
我有一个 .NET 2.0 windows 窗体应用程序,它大量使用了ListView
控件。
I've subclassed the ListView
class into a templated SortableListView<T>
class, so it can be a bit smarter about how it displays things, and sort itself.
我已将ListView
该类子类化为模板化SortableListView<T>
类,因此它可以更智能地显示事物并对其进行排序。
Unfortunately this seems to break the Visual Studio Forms Designer, in both VS2005 and 2008.
不幸的是,这似乎破坏了 VS2005 和 2008 中的 Visual Studio 窗体设计器。
The program compiles and runs fine, but when I try view the owning form in the designer, I get these Errors:
该程序编译并运行良好,但是当我尝试在设计器中查看拥有的表单时,出现以下错误:
- Could not find type 'MyApp.Controls.SortableListView'. Please make sure that the assembly that contains this type is referenced. If this type is a part of your development project, make sure that the project has been successfully built.
- 找不到类型“MyApp.Controls.SortableListView”。请确保引用了包含此类型的程序集。如果此类型是您的开发项目的一部分,请确保该项目已成功构建。
There is no stack trace or error line information available for this error
没有可用于此错误的堆栈跟踪或错误行信息
- The variable 'listViewImages' is either undeclared or was never assigned.
- 变量“listViewImages”要么未声明,要么从未分配过。
At MyApp.Main.Designer.cs Line:XYZ Column:1
在 MyApp.Main.Designer.cs 行:XYZ 列:1
Call stack:
at System.ComponentModel.Design.Serialization.CodeDomSerializerBase.Error(IDesignerSerializationManager manager, String exceptionText, String helpLink)
at System.ComponentModel.Design.Serialization.CodeDomSerializerBase.DeserializeExpression(IDesignerSerializationManager manager, String name, CodeExpression expression)
at System.ComponentModel.Design.Serialization.CodeDomSerializerBase.DeserializeExpression(IDesignerSerializationManager manager, String name, CodeExpression expression)
at System.ComponentModel.Design.Serialization.CodeDomSerializerBase.DeserializeStatement(IDesignerSerializationManager manager, CodeStatement statement)
The line of code in question is where it is actually added to the form, and is
有问题的代码行是它实际添加到表单的位置,并且是
this.imagesTab.Controls.Add( this.listViewImages );
listViewImages is declared as
listViewImages 声明为
private MyApp.Controls.SortableListView<Image> listViewImages;
and is instantiated in the InitializeComponent
method as follows:
并在InitializeComponent
方法中实例化如下:
this.listViewImages = new MyApp.Controls.SortableListView<Image>();
As mentioned earlier, the program compiles and runs perfectly, and I've tried shifting the SortableListView
class out to a seperate assembly so it can be compiled seperately, but this makes no difference.
正如前面提到的,程序编译并完美运行,我尝试将SortableListView
类移出到单独的程序集,以便可以单独编译,但这没有区别。
I have no idea where to go from here. Any help would be appreciated!
我不知道从这里去哪里。任何帮助,将不胜感激!
采纳答案by Orion Edwards
when you added the listview, did you add it to the toolbox and then add it to the form?
当您添加列表视图时,您是否将其添加到工具箱然后将其添加到表单中?
No, I just edited Main.Designer.cs
and changed it from System.Windows.Forms.ListView
to MyApp.Controls.SortableListView<Image>
不,我只是编辑Main.Designer.cs
并将其从 更改System.Windows.Forms.ListView
为MyApp.Controls.SortableListView<Image>
Suspecting it might have been due to the generics led me to actually finding a solution.
怀疑这可能是由于泛型导致我真正找到了解决方案。
For each class that I need to make a SortableListView for, I defined a 'stub class' like this
对于我需要为其创建 SortableListView 的每个类,我定义了一个这样的“存根类”
class ImagesListView : SortableListView<Image> { }
Then made the Main.Designer.cs
file refer to these stub classes instead of the SortableListView
.
然后使Main.Designer.cs
文件引用这些存根类而不是SortableListView
.
It now works, hooray!
它现在可以工作了,万岁!
Thankfully I am able to do this because all my types are known up front, and I'm only using the SortableListView
as a method of reducing duplicate code.
值得庆幸的是,我能够做到这一点,因为我所有的类型都是预先知道的,而且我只是将SortableListView
用作减少重复代码的方法。
回答by lomaxx
I've had a problem like this (tho not the same) in the past where my control was in a different namespace to my form even tho it was in the same project. To fix it I had to add a
过去我遇到过这样的问题(虽然不一样),但我的控件位于与表单不同的命名空间中,即使它在同一个项目中。为了解决它,我不得不添加一个
using My.Other.Namespace;
to the top of the designer generated code file. The annoying thing was it kept getting blown away when the designer regenerated the page.
到设计器生成的代码文件的顶部。令人讨厌的是,当设计师重新生成页面时,它一直被吹走。
回答by lomaxx
The assembly that contains MyApp.Controls.SortableListView isn't installed in the GAC by any chance is it?
包含 MyApp.Controls.SortableListView 的程序集没有安装在 GAC 中,是吗?
回答by lomaxx
when you added the listview, did you add it to the toolbox and then add it to the form?
当您添加列表视图时,您是否将其添加到工具箱然后将其添加到表单中?
回答by Unsliced
I had something similar - a user control was referring to a remote serice (which I couldn't guarantee being available at design time).
我有类似的东西 - 用户控件指的是远程服务(我不能保证在设计时可用)。
This post on MSDNsuggested that I add
MSDN上的这篇文章建议我添加
if (this.DesignMode) return;
to the Load function of the control, or in my case to the point before the WCF client was initialised. That did the trick.
到控件的 Load 函数,或者在我的情况下到 WCF 客户端初始化之前的点。这就是诀窍。
So
所以
private readonly Client _client = new Client();
becomes
变成
private Client _client;
public new void Load()
{
if(DesignMode) return;
_client = new Client();
}
回答by PahanCS
Perhaps you forgot to add that:
也许你忘了补充:
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Release all resources used.
/// </summary>
/// <param name="disposing">true if managed resources should be removed otherwise; false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
private void InitializeComponent()
{
// ...
this.components = new System.ComponentModel.Container(); // Not necessarily, if You do not use
// ...
}
回答by Mark Lakata
I had this problem too, related to merging massive SVN changes (with conflicts) in the *.Designer.cs file. The solution was to just open up the design view graphically, edit a control (move it to the left then right) and resave the design. The *.Designer.cs file magically changed, and the warning went away on the next compilation.
我也有这个问题,与在 *.Designer.cs 文件中合并大量 SVN 更改(有冲突)有关。解决方案是以图形方式打开设计视图,编辑控件(将其向左移动然后向右移动)并重新保存设计。*.Designer.cs 文件神奇地改变了,警告在下一次编译时消失了。
To be clear, you need to fix all of the code merge problems first. This is just a work around to force VS to reload them.
需要明确的是,您需要先修复所有代码合并问题。这只是强制 VS 重新加载它们的一种解决方法。
回答by cAko
In my case the problem was the folder's name of my project! Why i think this: I use SVN and in the 'trunk\SGIMovel' works perfectly. But in a branch folder named as 'OS#125\SGIMovel' I can't open the designer for a form that uses a custom control and works in the trunk folder.
就我而言,问题是我项目的文件夹名称!为什么我这么认为:我使用 SVN 并且在 'trunk\SGIMovel' 中完美运行。但是在名为“OS#125\SGIMovel”的分支文件夹中,我无法打开使用自定义控件并在主干文件夹中工作的表单的设计器。
Just get off the # and works nice.
刚下车 # 并且效果很好。
Thanks for nothing.
谢天谢地。
回答by Nila
I had the same issue. In my case this issue was due to resource initialization. I moved the following code from InitializeComponent
method to ctor(After calling InitializeComponent
). After that this issue was resolved:
我遇到过同样的问题。在我的情况下,这个问题是由于资源初始化。我将以下代码从InitializeComponent
方法移动到构造函数(调用后InitializeComponent
)。之后这个问题得到解决:
this->resources = (gcnew System::ComponentModel::ComponentResourceManager(XXX::typeid));
回答by Val
It happened to me because of x86 / x64 architecture.
由于 x86 / x64 架构,它发生在我身上。
Since Visual Studio (the development tool itself) has no x64 version, it's not possible to load x64 control into GUI designer.
由于 Visual Studio(开发工具本身)没有 x64 版本,因此无法将 x64 控件加载到 GUI 设计器中。
The best approach for this might be tuning GUI under x86, and compile it for x64 when necessary.
最好的方法可能是在 x86 下调整 GUI,并在必要时为 x64 编译它。