Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add WPF items into a C# class library project in VS2019?

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.

like image 831
Leoss Avatar asked Nov 13 '19 11:11

Leoss


People also ask

Can you mix WPF and Windows Forms?

Short Answer: Yes! Detailed Answer: It is possible to combine Win Forms and WPF in a single application.

Does Microsoft still support WPF?

Net Core 3.0. This demonstrates that Microsoft still has faith in WPF as a user interface framework, and the company is still willing to invest in it by updating and integrating it with its new offerings.” “Microsoft released WPF, WinForms, and WinUI on the same day it released.NET Core 3.0 Preview 1.

Can you use C++ with WPF?

You can use WPF with C++/CLI. It is a . NET API, however, so it requires the . NET Framework.


1 Answers

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.

like image 103
mm8 Avatar answered Oct 19 '22 22:10

mm8