Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 955
Charles Avatar asked Aug 15 '19 12:08

Charles


People also ask

Does .NET Core support WinForms?

NET Core and language features. Open source improvements to WPF and WinForms for . NET Core.

Can .NET Core use .NET framework libraries?

The answer is no, we cannot use . NET Framework Base Class Library in . NET Core because of compatibility issues. Basically, the libraries which target .


2 Answers

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

  • https://docs.microsoft.com/en-us/dotnet/desktop/winforms/migration/?view=netdesktop-5.0
  • https://docs.microsoft.com/en-us/dotnet/standard/frameworks
like image 78
J R Avatar answered Sep 20 '22 18:09

J R


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.

like image 21
Reza Aghaei Avatar answered Sep 20 '22 18:09

Reza Aghaei