.net 找不到类型“xxx.xxx.xxx”。请确保组装

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19009386/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 12:28:08  来源:igfitidea点击:

Could not find type 'xxx.xxx.xxx'. Please make sure that the assembly

.netvb.netvisual-studio-2012visual-studio-designer

提问by Andy Macnaughton-Jones

I've searched StackOverflow and found similar problems when I try and open a form that references a UserControl in a different project.

当我尝试打开一个引用不同项目中的 UserControl 的表单时,我搜索了 StackOverflow 并发现了类似的问题。

I get the

我得到

To prevent possible data loss before loading the designer, the following errors must be resolved:

为了防止在加载设计器之前可能丢失数据,必须解决以下错误:

message relating to the following two errors:

与以下两个错误相关的消息:

Could not find type 'MyNamespace.CommonUi.InformationBox'. 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 using settings for your current platform or Any CPU.

找不到类型“MyNamespace.CommonUi.InformationBox”。请确保引用了包含此类型的程序集。如果此类型是您的开发项目的一部分,请确保已使用当前平台或任何 CPU 的设置成功构建项目。

AND

The variable 'InformationBox1' is either undeclared or was never assigned.

变量 'InformationBox1' 要么未声明,要么从未分配过。

InformationBox1is an instance of the user control InformationBoxthat is on the form in the designer - it's just referenced as;

InformationBox1InformationBox设计器中表单上的用户控件的一个实例- 它只是被引用为;

Friend WithEvents InformationBox1 As MyNamespace.CommonUi.InformationBox

The MyNamespace.CommonUiproject builds successfully.

MyNamespace.CommonUi项目成功生成。

I get intellisense in the project so I'm reasonably confident that it's being referenced correctly.

我在项目中获得了智能感知,所以我有理由相信它被正确引用。

So far, the same as other people have had:

到目前为止,与其他人一样:

This is a project that's been migrated from .NET2/x86 in VS2005 to .NET4/x64 in VS2012.

这是一个从 VS2005 中的 .NET2/x86 迁移到 VS2012 中的 .NET4/x64 的项目。

Now when the solution is running under 64 bit it doesn't work and I get this designer error. However, if I switch it to 32 bit (well technically AnyCPU) I can open the designer just fine.

现在,当解决方案在 64 位下运行时,它不起作用,我收到此设计器错误。但是,如果我将它切换到 32 位(技术上是 AnyCPU),我可以很好地打开设计器。

I've read through the other suggestions in similar threads but they don't see to provide any solution (I've even gone as far as the "move it left & right to get it to rebuild" option)

我已经阅读了类似线程中的其他建议,但他们没有看到提供任何解决方案(我什至选择了“向左和向右移动以使其重建”选项)

回答by rboy

I believe the issue is that Visual Studio is natively 32bitand one cannot do GUI editing for some components (E.g. ListView) in 64bit. E.g. on forms where you have ListView, you need to change the solution to 32bit to edit the GUI.

我认为问题在于 Visual Studio本身32 位的,并且无法对 64 位的某些组件(例如 ListView)进行 GUI 编辑。例如,在具有 ListView 的表单上,您需要将解决方案更改为 32 位以编辑 GUI。

So the answer in short is, when you're facing this issue:

简而言之,当您遇到此问题时,答案是:

  1. Change the solution to 32bit or AnyCPU
  2. Clean and rebuild the solution/project
  3. Open the GUI for editing
  4. Save, change the solution back to 64bit
  5. Clean and rebuild
  6. Run in 64bit
  1. 将解决方案改为32bit或AnyCPU
  2. 清理并重建解决方案/项目
  3. 打开 GUI 进行编辑
  4. 保存,将解决方案改回64bit
  5. 清洁和重建
  6. 以 64 位运行

Unfortunately Visual Studio doesn't come in 64bitas yet, hence all controls need to be designed in 32bit mode (or AnyCPU mode).

不幸的是,Visual Studio还没有 64 位,因此所有控件都需要在 32 位模式(或 AnyCPU 模式)下设计。

See this question for more details.

有关更多详细信息,请参阅此问题。

VS 2010 designer error 'Could not find type XYZ' in Windows7. Works fine in XP

VS 2010 设计器错误“在 Windows7 中找不到类型 XYZ”。在 XP 中运行良好

回答by Anton Purin

I had this problem. It happened only in one form designer view, despite it was able to compile, start, show this form in runtime and show other forms / controls in designer mode.

我有这个问题。它只发生在一个表单设计器视图中,尽管它能够在运行时编译、启动、显示此表单并在设计器模式下显示其他表单/控件。

These steps didn't help:

这些步骤没有帮助:

  • Clean & rebuild
  • Restart studio
  • Deleting all bin and obj directories
  • Removing and adding references
  • Denial, anger, bargaining, depression, acceptance
  • 清洁和重建
  • 重启工作室
  • 删除所有 bin 和 obj 目录
  • 删除和添加引用
  • 否认、愤怒、讨价还价、沮丧、接受

Solutionfor my case:

我的情况的解决方案

  1. Rename a type, which is missing (for example InformationBox => InformationBox2)
  2. Refresh designer (WOW, it works!)
  3. Rename the type to its initial name
  1. 重命名缺少的类型(例如 InformationBox => InformationBox2)
  2. 刷新设计师(哇,它有效!)
  3. 将类型重命名为其初始名称

回答by jcwrequests

Change Any CPU to X86. Your control is 32bit trying to run on 64bit machine and cannot find the 64bit version of the control.

将任何 CPU 更改为 X86。您的控件是 32 位的,试图在 64 位机器上运行,但找不到 64 位版本的控件。

回答by Taegost

I had this same error recently with VB.Net in Visual Studio 2013 working with a custom WinForm User Control that itself was inheriting a custom user control base class in the same project, and took some doing to find out what the true cause was, which in my case was that neither base class nor the child class had a no-parameter constructor (Because that's not a valid scenario in this case).
To fix it, I added the missing constructor but left it empty (throwing a NotImplementedException caused another issue that prevented it from displaying). It's not pretty, but it works.

我最近在 Visual Studio 2013 中的 VB.Net 中遇到了同样的错误,使用自定义 WinForm 用户控件,该控件本身继承了同一项目中的自定义用户控件基类,并采取了一些措施来找出真正的原因,这是就我而言,基类和子类都没有无参数构造函数(因为在这种情况下这不是有效的场景)。
为了修复它,我添加了缺少的构造函数,但将其留空(抛出 NotImplementedException 导致了另一个阻止它显示的问题)。它不漂亮,但它有效。

In order to see the underlying error through the one listed in this threads question, I had to do the following:

为了通过此线程问题中列出的错误查看潜在错误,我必须执行以下操作:

  1. Clean the entire solution
  2. Close Visual Studio
  3. Re-open Visual Studio
  4. Re-open the solution
  5. Build the solution by right-clicking it in Solution Explorer (Not Rebuild, that didn't work)
  6. View the User Control in Designer mode and the actual error shows up now
  1. 清洁整个溶液
  2. 关闭 Visual Studio
  3. 重新打开 Visual Studio
  4. 重新打开解决方案
  5. 通过在解决方案资源管理器中右键单击解决方案来构建解决方案(不是重建,那不起作用)
  6. 在设计器模式下查看用户控件,现在显示实际错误

After adding the constructor, I had to take the above steps again to make it work properly.

添加构造函数后,我不得不再次执行上述步骤才能使其正常工作。

回答by OldDog

I changed a large solution with multiple projects to target x64 platform from AnyCPU. Tried opening the designer of one of the solution forms which referenced a control in one of the other projects and got the same error message as the OP. Opened the project that contained the control and saw it still targeted AnyCPU. Tried for an hour to get it to save as x64 with no luck. I eventually opened the csproj file in notepad, replaced AnyCPU with x64 and it all started working. Hopefully this helps someone else as frustrated as I was.

我将一个包含多个项目的大型解决方案从 AnyCPU 更改为面向 x64 平台。尝试打开其中一个解决方案表单的设计器,该表单引用了其他项目之一中的控件,并得到了与 OP 相同的错误消息。打开包含控件的项目,发现它仍然针对 AnyCPU。尝试了一个小时以将其保存为 x64,但没有运气。我最终在记事本中打开了 csproj 文件,用 x64 替换了 AnyCPU,一切都开始工作了。希望这可以帮助像我一样沮丧的其他人。

回答by Andy Macnaughton-Jones

Well here's some further information: the-designer-could-not-be-shown-with-platform-x64

那么这里有一些进一步的信息: the-designer-could-not-be-shown-with-platform-x64

And the resolution to run in AnyCPU, when you are trying to access the designer is a work-around and for our purposes will suffice.

当您尝试访问设计器时,在 AnyCPU 中运行的解决方案是一种变通方法,对于我们的目的就足够了。

回答by Whirl Mind

While there is a lot of reference to 32-bit etc, the steps that worked for me were :

虽然有很多关于 32 位等的参考,但对我有用的步骤是:

  • Convert all references to the user control, say, 'InformationBox1 as InformationBox' into a fully qualified class reference such as 'MyNamespace.CommonUi.InformationBox', in all the designer.vb files.

    • Clean Solution

    • Rebuild Solution.

  • 在所有designer.vb 文件中,将对用户控件的所有引用(例如“InformationBox1 as InformationBox”)转换为完全限定的类引用,例如“MyNamespace.CommonUi.InformationBox”。

    • 清洁溶液

    • 重建解决方案。

In my case, it was a migration project from VB6 to VS2008 and both the environments were 32-bit, and on the same machine, there was no sign of 64-bit involved.

就我而言,这是一个从 VB6 到 VS2008 的迁移项目,并且两个环境都是 32 位的,并且在同一台机器上,没有涉及 64 位的迹象。

回答by Shamal Sabah

Just save your project, close it then reopen it again.

只需保存您的项目,关闭它然后再次重新打开它。

回答by TLT

Recently I encountered the same problem with one of my custom controls (C#) in VS 2015.

最近我在 VS 2015 中遇到了与我的自定义控件(C#)之一相同的问题。

I solved it by cleaning the solution (Build -> Clean Solution) and then rebuild the whole solution. Everything came back smoothly.

我通过清理解决方案(Build -> Clean Solution)然后重建整个解决方案来解决它。一切都顺利地回来了。

My project settings' platform target is already set to "Any CPU" and checkbox ticked for "Prefer 32-bit". Not sure why it happened though.

我的项目设置的平台目标已设置为“任何 CPU”,并勾选“首选 32 位”复选框。不知道为什么会这样。

回答by AMMAR ELHAMDO

I have the same problem and I solved it by:

我有同样的问题,我通过以下方式解决了它:

  1. Going to properties of your solution in visual studio.
  2. Chaging 'Platform' to 'AnyCPU'.
  3. Rebuilding your solution.
  4. Restarting visual studio.
  1. 在 Visual Studio 中转到解决方案的属性。
  2. 将“平台”更改为“AnyCPU”。
  3. 重建您的解决方案。
  4. 重新启动视觉工作室。