简体   繁体   English

如何使Visual Studio 2010的“添加用户控件”创建WPF控件?

[英]How can I make Visual Studio 2010's “Add User Control” create a WPF control?

I have a class library that I created using the "Class Library" project template. 我有一个使用“类库”项目模板创建的类库。 If I right-click on this library in Solution Explorer and select "Add > User Control", Visual Studio adds a WinForms UserControl. 如果我在解决方案资源管理器中右键单击此库并选择“添加>用户控件”,Visual Studio将添加WinForms UserControl。 That's not what I want -- I want "Add > User Control" to add a WPF user control. 这不是我想要的 - 我想要“添加>用户控件”来添加WPF用户控件。

I've already added references to the WPF assemblies (WindowsBase, PresentationFramework, and PresentationCore), and I already have some WPF UserControls in this library, and everything compiles. 我已经添加了对WPF程序集(WindowsBase,PresentationFramework和PresentationCore)的引用,我已经在这个库中有一些WPF UserControl,并且所有内容都会编译。 My library does not have references to the WinForms assemblies (System.Drawing and System.Windows.Forms). 我的库没有对WinForms程序集的引用(System.Drawing和System.Windows.Forms)。 But apparently the proper references are not enough of a clue for Visual Studio, because when I try Add > User Control, it adds the WinForms references to my project, and then creates a WinForms UserControl. 但显然正确的引用并不足以成为Visual Studio的线索,因为当我尝试添加>用户控件时,它会将WinForms引用添加到我的项目中,然后创建一个WinForms UserControl。

I can add a WPF User Control to my WPF Application project, and then move it into my library. 我可以将WPF用户控件添加到我的WPF应用程序项目中,然后将其移动到我的库中。 But that's a pain, and I'd rather have it work properly in the first place. 但这是一种痛苦,我宁愿让它在第一时间正常工作。

I think I'm probably missing some kind of arcane XML element in my .csproj file that tells Visual Studio which designer to use by default, and if I add the right XML element with the right cryptic GUID, it will start working properly. 我想我可能在我的.csproj文件中遗漏了某种神秘的XML元素,告诉Visual Studio默认使用哪个设计器,如果我添加正确的XML元素和正确的神秘GUID,它将开始正常工作。 If I could create a new WPF Control Library, I could probably compare the two project files and figure this out. 如果我可以创建一个新的WPF控件库,我可能会比较这两个项目文件并想出来。 However, I'm using Visual C# Express, which doesn't have a template for a WPF Control Library project, so I'm out of luck there. 但是,我使用的是Visual C#Express,它没有WPF控件库项目的模板,所以我在那里运气不好。

What do I need to do to my Class Library's .csproj file so that VS2010's Add > New User Control will add a WPF UserControl? 我需要对类库的.csproj文件做什么,以便VS2010的添加>新用户控件将添加WPF UserControl?

There are sub-projects class ids in the project file that affect the Visual Studio context menus and how the project behaves in general. 项目文件中有子项目类ID,它们影响Visual Studio上下文菜单以及项目的行为方式。 The easiest thing to do is to recreate the project as a: 最简单的方法是将项目重新创建为:

  • WPF User Control Library WPF用户控件库

instead of a "Class Library". 而不是“类库”。 It is possible if you already created the project to edit in the sub-project class ids by hand by opening the ".csproj" file in a text editor such as Visual Studio itself but its easy to cause more damage than you fix that way. 如果您已经通过在文本编辑器(如Visual Studio本身)中打开“.csproj”文件来手动创建要在子项目类ID中编辑的项目,但它很容易造成比您修复方式更多的损坏。

I believe but haven't test that another type of library will also work: 我相信但是没有测试另一种类型的库也可以工作:

  • WPF Custom Control Library WPF自定义控件库

which is intended to hold other types of controls than UserControl objects but being a WPF sub-project type the context menus also work correctly for the use case you are describing. 它旨在保存除UserControl对象之外的其他类型的控件,但是作为WPF子项目类型,上下文菜单也适用于您正在描述的用例。

Edit: 编辑:

For completeness, I've just tested how to manually add the sub-project GUIDS. 为了完整起见,我刚刚测试了如何手动添加子项目GUIDS。 Add this line to the first PropertyGroup in the .csproj file: 将此行添加到.csproj文件中的第一个PropertyGroup

  <PropertyGroup>
    ...
    <ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
  </PropertyGroup>

Not tested with Visual Studio Express. 未使用Visual Studio Express进行测试。

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

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