Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use System.Windows.Forms in .NET Core class library

I've created .NET Core class library and try to build it against net40 framework. I want to use Clipboard class from System.Windows.Forms assembly. How can I do this?

My project.json file:

{
    "version": "1.0.0-*",

    "dependencies": {
        "NETStandard.Library": "1.6.0"
    },

    "frameworks": {
        "netstandard1.6": {
            "imports": "dnxcore50",
            "buildOptions": {
                "define": [
                    "NETCORE"
                ]
            },
            "dependencies": {
                "System.Threading": "4.0.11",
                "System.Threading.Thread": "4.0.0",
                "System.Threading.Tasks":  "4.0.11"
                }
        },
        "net40": {
            "buildOptions": {
                "define": [
                    "NET40"
                    ]
                },
            "dependencies": {
                // dependency should be here but there is no such dll
            }
        }
    }
}

All my net40 specific code is under NET40 define. Any thoughts?

like image 206
Alexander Buts Avatar asked Jul 19 '16 13:07

Alexander Buts


People also ask

Can you use Windows Forms in .NET core?

Today we're happy to announce that the Windows Forms designer for . NET Core projects is now available as a preview in Visual Studio 2019 version 16.6! We also have a newer version of the designer available in Visual Studio 16.7 Preview 1!

How do I add a Windows form to a class library?

You can right click on project name, click on Add and then in Windows form. It should add required References for your form.

How do I add a system window form?

Right-click your project in Solution Explorer and select Add reference... and then find System. Windows. Forms and add it.


3 Answers

For VS2019 .NET Core 3.1:

  1. right-mouse click on the project and select Unload Project
  2. right-mouse click on the project and select "Edit foobar.csproj"
  3. Example of using WPF and Winforms in .NET Core 3.1: where I added the UseWPF and UseWindowsForms tags. Also I changed Microsoft.NET.Sdk to Microsoft.NET.Sdk.WindowsDesktop to be able to use also wpf.
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">

  <PropertyGroup Label="Globals">
    <SccProjectName>SAK</SccProjectName>
    <SccProvider>SAK</SccProvider>
    <SccAuxPath>SAK</SccAuxPath>
    <SccLocalPath>SAK</SccLocalPath>
  </PropertyGroup>

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <UseWPF>true</UseWPF>
    <UseWindowsForms>true</UseWindowsForms>
  </PropertyGroup>
...
  1. save and right-mouse click on the project again and select Reload Project
like image 197
juFo Avatar answered Oct 10 '22 08:10

juFo


for .Net 6 the .csproj file should contain:

<PropertyGroup>
     <TargetFramework>net6.0-windows</TargetFramework>
     <UseWPF>true</UseWPF>
     <UseWindowsForms>true</UseWindowsForms>
</PropertyGroup>

Note the "-windows" in the target framework.

UseWPF is optional.

like image 28
Louis Somers Avatar answered Oct 10 '22 08:10

Louis Somers


What you need is "frameworkAssemblies", for example:

"frameworks": {
  "netstandard1.6": {
    "dependencies": {
      "NETStandard.Library": "1.6.0"
    }
  },
  "net40": {
    "frameworkAssemblies": {
      "System.Windows.Forms": {}
    }
  }
}

Working with Clipboard also requires setting the main thread as STA, so don't forget to add [STAThread] to Main() in your application.

like image 41
svick Avatar answered Oct 10 '22 10:10

svick