问题描述
我已经在 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(因为我只尝试使用我以前使用过的那些).
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?
附言我知道这个问题之前似乎已经回答过(例如 没有在 DLL 项目中创建 WPF 窗口),但据我所知,它们都适用于以前版本的 Visual Studio,这些建议对我不起作用.
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# 类库项目中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!