8

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.

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).

Does anyone know the correct process to follow for VS2019?

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.

Pang
  • 9,564
  • 146
  • 81
  • 122
Leoss
  • 143
  • 1
  • 7

1 Answers1

17

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>

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>

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

Vlad
  • 35,022
  • 6
  • 77
  • 199
mm8
  • 163,881
  • 10
  • 57
  • 88
  • 2
    This worked a treat, thank you! Incidentally I also noticed my target frame work was "netstandard2.0" so that got me thinking. Your fix also works if I put "net472" in the target framework element as I was supposed to be using .NET Framework 4.7.2. I investigated further and realised I had initially used the template for "Class Library (.NET Standard)" instead of "Class Library (.NET Framework)". Creating a class library using the .NET Framework template allows me to edit the template in the way mentioned in my original post...just in case that helps someone, but great to know the new way! – Leoss Nov 13 '19 at 13:30
  • 1
    If you target .NET Framwork, you might as well use the `WPF User Control Library` or the `WPF Custom Controls Library` template. – mm8 Nov 13 '19 at 13:31
  • Thanks, yes, although I'm not just adding WPF code. It is a more general library that includes some WPF - possibly not the best way to do it but I'm pretty new to figuring out good architecture :( I think the initial problem came from me not realising there were multiple c# class library templates, sorry – Leoss Nov 13 '19 at 13:38
  • 1
    This works great! I only added the `true` element and the correct menu items were available without reloading the project. – Xtr Jan 17 '20 at 13:20
  • I was trying to add just the UseWPF tag and couldn't figure out why it wasn't working. That Sdk setting really makes all the difference! – Matt Gregory Apr 27 '20 at 14:28