'Use Windows Forms in a .Net Core Class Library - .NET Core Control Library

I am trying to create a .net core 3 class library that references the .net core 3 version of winform (so this assembly can itself be referenced by a .net core 3 WinForm assembly).

A new .net core WinForm project references Microsoft.WindowsDesktop.App.WindowsForms, however I can't find any nuget package with that name.

What do I need to do to reference the .net core 3 winform?



Solution 1:[1]

I had a core 3.1 web app referring to a Framework 4.5.2 project that had dependencies on System.Windows.Forms. The fix for me was to add the below line to web app csproj file:

FrameworkReference Include="Microsoft.WindowsDesktop.App.WindowsForms"

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

  <PropertyGroup>
    <TargetFrameworks>netcoreapp3.1</TargetFrameworks>
    <UseWindowsForms>true</UseWindowsForms>
  </PropertyGroup>

  <ItemGroup>
    <FrameworkReference Include="Microsoft.WindowsDesktop.App.WindowsForms" />
  </ItemGroup>

</Project>

Solution 2:[2]

The currently accepted answer appears to be somewhat outdated. The recent syntax requires the target to be specified in the TargetFramework tag, not in the Sdk tag:

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

  <PropertyGroup>
    <OutputType>Library</OutputType>
    <TargetFramework>net5.0-windows</TargetFramework>
    <UseWindowsForms>true</UseWindowsForms>
    <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
  </PropertyGroup>

</Project>

More info here

Solution 3:[3]

In alternative you can add a new project of Windows Form Type and than set the output type to Libray.

<OutputType>Library</OutputType>

Solution 4:[4]

For me, with net5 I needed the following in my project

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

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 J R
Solution 3 Roberto Bergamini
Solution 4 Kirsten