如何在VS2019中将WPF项目添加到C#类库项目中? [英] How can I add WPF items into a C# class library project in VS2019?

查看:49
本文介绍了如何在VS2019中将WPF项目添加到C#类库项目中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Visual Studio 2019中启动了一个类库项目,现在希望向其中添加WPF项(窗口,用户控件,自定义控件),但是``添加项''对话框在WPF部分下未列出任何内容

I have started a Class library project in Visual Studio 2019 and now wish to add WPF items (Window, user control, custom control) into it, but the 'Add Item' dialog box doesn't list anything under the WPF section.

我在VS的早期版本中遇到了这个问题,并设法通过将< ProjectTypeGuids> 元素添加到具有相关WPF GUID的csproj文件中来解决了这个问题.但是,这似乎不适用于新的VS2019精简的csproj文件,或者我不知道在哪里可以找到正确的GUID(因为我仅尝试使用以前使用的GUID).

I have come across this problem in previous versions of VS and managed to get around it by adding the <ProjectTypeGuids> element into the csproj file with the relevant WPF GUIDs. However, that doesn't appear to work with the new VS2019 stripped down csproj file, or I don't know where to find the correct GUIDs (as I have only tried using those I used before).

有人知道VS2019应该遵循的正确过程吗?

Does anyone know the correct process to follow for VS2019?

P.S.我知道这个问题以前似乎已经得到回答(例如

P.S. I am aware that this question appears to have been answered before (for example No creation of a WPF window in a DLL project), but as far as I can tell, they are all for previous versions of Visual Studio and the suggestions don't work for me.

推荐答案

对于.NET Core项目,右键单击类库项目,选择编辑项目文件",然后将以下内容复制到.csproj 文件:

For .NET Core projects, right-click on the class library project, choose "Edit project file", and copy the following contents into the .csproj file:

<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">

  <PropertyGroup>
    <TargetFramework>netcoreapp3.0</TargetFramework>
    <UseWPF>true</UseWPF>
  </PropertyGroup>

</Project>

对于.NET 5项目,您需要一些不同的内容:

For .NET 5 projects you would need a bit different content:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net5.0-windows</TargetFramework>
    <UseWPF>true</UseWPF>
  </PropertyGroup>

</Project>

这应该使您能够使用菜单将WPF特定项目添加到项目中.

This should give you the ability to add WPF specific items to the project using the menus.

这篇关于如何在VS2019中将WPF项目添加到C#类库项目中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆