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

发布于 2024-11-07 22:00:02 字数 789 浏览 4 评论 0原文

我有一个使用“类库”项目模板创建的类库。如果我在解决方案资源管理器中右键单击该库并选择“添加 > 用户控件”,Visual Studio 将添加一个 WinForms UserControl。这不是我想要的——我想要“添加>用户控件”来添加 WPF 用户控件。

我已经添加了对 WPF 程序集(WindowsBase、PresentationFramework 和PresentationCore)的引用,并且该库中已经有一些 WPF UserControl,并且所有内容都可以编译。我的库没有对 WinForms 程序集(System.Drawing 和 System.Windows.Forms)的引用。但显然正确的引用不足以为 Visual Studio 提供足够的线索,因为当我尝试 Add >用户控件,它将 WinForms 引用添加到我的项目中,然后创建一个 WinForms UserControl。

我可以将 WPF 用户控件添加到我的 WPF 应用程序项目,然后将其移动到我的库中。但这很痛苦,我宁愿让它首先正常工作。

我认为我的 .csproj 文件中可能缺少某种神秘的 XML 元素,它告诉 Visual Studio 默认使用哪个设计器,如果我添加带有正确的神秘 GUID 的正确 XML 元素,它将开始正常工作。如果我可以创建一个新的 WPF 控件库,我可能可以比较两个项目文件并找出答案。但是,我使用的是 Visual C# Express,它没有 WPF 控件库项目的模板,所以我不太走运。

我需要对我的类库的 .csproj 文件做什么,以便 VS2010 的 Add >新的User Control会添加一个WPF UserControl吗?

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. That's not what I want -- I want "Add > User Control" to add a WPF user control.

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. My library does not have references to the WinForms assemblies (System.Drawing and 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.

I can add a WPF User Control to my WPF Application project, and then move it into my library. 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. If I could create a new WPF Control Library, I could probably compare the two project files and figure this out. 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.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

人海汹涌 2024-11-14 22:00:02

项目文件中的子项目类 ID 会影响 Visual Studio 上下文菜单以及项目的一般行为方式。最简单的方法是将项目重新创建为:

  • WPF 用户控件库

而不是“类库”。如果您已经创建了要在子项目类 id 中编辑的项目,则可以通过在文本编辑器(例如 Visual Studio 本身)中打开“.csproj”文件来手动编辑,但它很容易造成比您通过这种方式修复更多的损坏。

我相信但尚未测试另一种类型的库也可以工作:

  • WPF 自定义控件库,

旨在容纳除 UserControl 对象之外的其他类型的控件,但作为 WPF 子项目类型的上下文菜单也适用于您所描述的用例。

编辑:

为了完整起见,我刚刚测试了如何手动添加子项目 GUIDS。将此行添加到 .csproj 文件中的第一个 PropertyGroup

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

未使用 Visual Studio Express 进行测试。

There are sub-projects class ids in the project file that affect the Visual Studio context menus and how the project behaves in general. The easiest thing to do is to recreate the project as a:

  • WPF User Control Library

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.

I believe but haven't test that another type of library will also work:

  • WPF Custom Control Library

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.

Edit:

For completeness, I've just tested how to manually add the sub-project GUIDS. Add this line to the first PropertyGroup in the .csproj file:

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

Not tested with Visual Studio Express.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文