简体   繁体   English

无法在表单上放置用户控件

[英]Cannot Place User Control on Form

I've created a C# WinForms application using VS2010. 我使用VS2010创建了一个C#WinForms应用程序。 I'm new to creating user controls so I created a new user control (as part of the same project). 我是创建用户控件的新手,所以我创建了一个新的用户控件(作为同一项目的一部分)。

When I rebuild the project, the new control appears in the toolbox. 重建项目时,新控件将显示在工具箱中。 And when I drag the control from the toolbox onto a form, I get the following error. 当我将控件从工具箱拖到窗体上时,我收到以下错误。

Failed to load toolbox item 'TagGroup'. 无法加载工具箱项“TagGroup”。 It will be removed from the toolbox. 它将从工具箱中删除。

This happened the only other time I created a user control as well. 这是我创建用户控件的唯一时间。 I've searched the web but most answers I found seemed related to having the control in a separate assembly. 我在网上搜索过,但我发现的大多数答案似乎与将控件放在一个单独的程序集中有关。 (Note that I found plenty of questions with the same problem I'm having.) (请注意,我发现了很多与我遇到的问题相同的问题。)

Can anyone suggest where I should look next? 任何人都可以建议我下一步要看哪里?

My application need to be 64-bit. 我的应用程序需要是64位。 In order to use custom user controls in the designer I just added a new project to my solution. 为了在设计器中使用自定义用户控件,我刚刚在我的解决方案中添加了一个新项目。 This new project use the "AnyCPU" setting and contains all my user controls. 这个新项目使用“AnyCPU”设置并包含我的所有用户控件。

My solution contains the following projects: 我的解决方案包含以下项目:

  • MyApp which is my main project (Windows Form Application) compiled in 64-bit and referencing my second project MyApp是我的主要项目(Windows窗体应用程序),以64位编译并引用我的第二个项目
  • MyApp.UI.UserControls (class library) is compiled for " Any CPU " and contains all my user controls MyApp.UI.UserControls (类库)是为“ 任何CPU ”编译的,包含我的所有用户控件

Works like a charm and it's clean 像魅力一样工作,很干净


By the way, there is a Microsoft support article about that issue. 顺便说一下,有一篇关于该问题的Microsoft支持文章

  • Action: You attempt to use a 64-bit component within the Microsoft Visual Studio Integrated Development Environment (IDE). 操作:您尝试在Microsoft Visual Studio集成开发环境(IDE)中使用64位组件。
  • Error cause: This behavior is by design. 错误原因:此行为是设计使然。 Visual Studio is a 32-bit process, and therefore can only execute 32-bit modules. Visual Studio是一个32位进程,因此只能执行32位模块。 While Visual Studio allows you to add a reference to a 64-bit assembly, it cannot actually JIT compile it to 64-bit and execute it in process. 虽然Visual Studio允许您添加对64位程序集的引用,但它实际上无法JIT将其编译为64位并在进程中执行它。
  • Resolution: 解析度:
    1. Rebuild the assembly using the "AnyCPU" setting. 使用“AnyCPU”设置重建装配。 This would allow the component to run within a 32-bit process (such as Visual Studio), or in a 64-bit process. 这将允许组件在32位进程(例如Visual Studio)或64位进程中运行。
    2. Add the assembly as a reference and load the control dynamically at run-time. 添加程序集作为参考,并在运行时动态加载控件。 Although you still would be unable to use the control within any designer inside Visual Studio, you can still write the code needed to instantiate the control and set it's properties accordingly. 虽然您仍然无法在Visual Studio中的任何设计器中使用该控件,但您仍然可以编写实例化控件所需的代码并相应地设置它的属性。

Source: http://support.microsoft.com/kb/963017 资料来源: http //support.microsoft.com/kb/963017

I finally figured this one out. 我终于想出了这一个。

The project I'm working with uses two class-library assemblies. 我正在使用的项目使用两个类库程序集。 Although these have nothing to do with the control I'm discussing, I looked and saw both libraries have Platform Target in the Properties|Build tab set to "Any CPU". 虽然这些与我正在讨论的控件无关,但我查看并看到两个库在Properties | Build选项卡中将Platform Target设置为“Any CPU”。

On the other hand, my application had this setting set to "x64". 另一方面,我的应用程序将此设置设置为“x64”。 By changing my application's setting to "Any CPU", I can now place my user controls onto my forms. 通过将我的应用程序的设置更改为“任何CPU”,我现在可以将我的用户控件放在我的表单上。

Go figure... 去搞清楚...

I had this problem too, but the answer couldn't fit for me. 我也有这个问题,但答案不适合我。 My project has some issues it can only target x86 and x64 separately. 我的项目有一些问题,它只能分别针对x86和x64。 In other words, I can't use AnyCPU configuration (that's because I reference different libraries for each configuration, since those libraries are not fit to AnyCPU). 换句话说,我不能使用AnyCPU配置(因为我为每个配置引用了不同的库,因为这些库不适合AnyCPU)。

The solution I came up with was: when I need to use the form designer, I change the setting to x86. 我想出的解决方案是:当我需要使用表单设计器时,我将设置更改为x86。 Do the job, then set back to x64 and test. 完成工作,然后重新设置为x64并进行测试。 The problem occurs only with the designer, but the solution builds and runs fine. 问题只发生在设计人员身上,但解决方案构建并运行良好。

I had this problem in VS2015 and the solution turned out to be simple. 我在VS2015中遇到了这个问题,解决方案很简单。

I had created a user control by cutting and pasting a few existing controls from a form (with the aim of grouping them into the custom control). 我通过从表单中剪切和粘贴一些现有控件来创建用户控件(目的是将它们分组到自定义控件中)。 The custom control was okay (no compile errors), however the removal of the controls from the existing form meant that the application wouldn't compile. 自定义控件没问题(没有编译错误),但是从现有表单中删除控件意味着应用程序无法编译。 Of course not being able to add the new control meant that I couldn't update the code referencing the previous controls with code referencing the custom control. 当然,无法添加新控件意味着我无法使用引用自定义控件的代码更新引用先前控件的代码。

All I did was hack and slash (commenting-out, creating temporary controls, etc.) so that the entire application would compile. 我所做的就是破解和删减(注释掉,创建临时控件等),以便整个应用程序可以编译。 After it compiled I found that I could drag the custom control onto the form (without the error that prompted this question). 编译后我发现我可以将自定义控件拖到窗体上(没有提示此问题的错误)。 I then had to unhack and unslash so that the code properly referenced the new custom control. 然后我不得不unhack和unslash,以便代码正确引用新的自定义控件。

Same problem here. 同样的问题在这里 I am guessing it is related to the fact that the VS2010 installed on x64 OS is still a 32bit program in the heart. 我猜这与安装在x64操作系统上的VS2010仍然是一个32位程序的事实有关。

An alternative solution one might want to try is simply open TheFormThisUserControlIsSupposedToBeAddedTo.Designer.cs and use code to add the user control. 可能想要尝试的替代解决方案是打开TheFormThisUserControlIsSupposedToBeAddedTo.Designer.cs并使用代码添加用户控件。 Basically, you are doing the dirty work that the Designer is supposed to do. 基本上,你正在做设计师应该做的肮脏的工作。

It is not as difficult as it sounds, esp. 它并不像听起来那么困难,尤其是 considering that there are probably plenty of sample code in that file already (eg the buttons you added using Designer). 考虑到该文件中可能已有大量示例代码(例如,您使用Designer添加的按钮)。 The only difficult part is figuring out the right coordinates in the form to place the control. 唯一困难的部分是确定表格中的正确坐标以放置控件。

The end result is that you can not see the user control in Desinger, but they are added on debugging/running. 最终结果是您无法在Desinger中看到用户控件,但它们会在调试/运行时添加。

I also experienced this problem but the cause was different. 我也遇到过这个问题,但原因不同了。 In my case a component (form) constructor or Load event invoked a method elsewhere that used reflection to find all classes that implemented a certain interface. 在我的例子中,组件(表单)构造函数或Load事件调用了其他地方的方法,该方法使用反射来查找实现某个接口的所有类。

While that works fine at runtime, it generated the above mentioned exception at Design Time. 虽然这在运行时工作正常,但它在设计时生成了上述异常。 (Type Initialization exception with a Type Load Exception as inner exception). (键入初始化异常,并将类型加载异常作为内部异常)。

An hour of confused, weary troubleshooting at 3:36AM is again easily resolved with a fresh mind the next day! 凌晨3:36,一小时的困惑,疲惫的故障排除再次轻松解决,第二天就有了新的头脑!
I fixed a simple spelling mistake in the control's filename, so it now matched the UserControl name, cleaned and re-built and Bobs your weird uncle's cat. 我在控件的文件名中修复了一个简单的拼写错误,所以它现在匹配了UserControl名称,清理并重新构建了Bobs你怪异的叔叔的猫。 :] :]

GO项目属性然后'Build'选项卡并检查'Target Platform'是'AnyCPU'。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM