19

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?

3
  • Is "net core 3 winform" a thing? Are you trying to migrate old winforms application into .net core? Commented Aug 15, 2019 at 13:37
  • It looks very beta to me, Google say release is September 2019. Without form designer.. kek. Commented Aug 15, 2019 at 13:45
  • I am trying to write from scratch (not migrating) a WinForm helper assembly to be shared between .net core 3 WinForm applications. There is a UI to create a .net core 3 WinForm application. I just can't find a way to create a .net core 3 class assembly referencing WinForm. Commented Aug 15, 2019 at 13:50

5 Answers 5

30

Update: In later versions of VS 2019 (I tried Version 16.8.2) there is a project template for Windows Forms Control Library for .NET Core.

Currently Windows Forms .NET Core is in Preview mode and I don't know any official Nuget package or Project template for Windows Forms Control Library in .NET Core in VS 2019 16.2.2.

But to create a Windows Forms Control Library, you can use the following instructions:

  1. Add a new project of type Class Library (.NET Core)
  2. After project created, right click on project file and choose Edit Project File
  3. Change the project SDK to <Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
  4. Specify windows forms as UI technology by adding <UseWindowsForms>true</UseWindowsForms>.

Now the project file should be like the following:

<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
  <PropertyGroup>
    <OutputType>Library</OutputType>
    <TargetFramework>netcoreapp3.0</TargetFramework>
    <UseWindowsForms>true</UseWindowsForms>
  </PropertyGroup>
</Project>

Now you can add Windows forms elements like Form or UserControl to this project and build the project without any problem.

Sign up to request clarification or add additional context in comments.

13 Comments

If for some reason you are looking for dlls, you can find them in C:\Program Files\dotnet\packs\Microsoft.WindowsDesktop.App.Ref\3.0.0-preview6-27626-01\ref\netcoreapp3.0 (depending to installed version of the framework).
I tried your suggestion but the "System.Windows.Forms" namespace isn't made available in that project.
Hello Reza, great answer and it's working for netcoreapp3.0. I had the feeling i had to close visual studio once before it started working. Do you know a way to also have it work for .netstandard2.0 and .netstandard2.1. I haven't been able to do that yet.
@JRB Did you ever figure out how to do this with Standard 2.1?
@JRB Thanks for the update. That does appear to be the right thing to do to get winforms to work; class assembly for core not standard
|
10

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>

Comments

8

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

Comments

3

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

<OutputType>Library</OutputType>

Comments

2

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.