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

4

46

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?

Grappa answered 19/7, 2016 at 13:38 Comment(3)
Yes, you are right. But also you can create simple console application with .NET Core. I tried to create a custom console for my needs with copy/paste functionality, so I need to copy data from clipboard and paste it to my console.Grappa
So you have to found another Class which is part of the .NET Core to work with the clipboard or switch to a Console App which target "Legacy .Net".Mezzosoprano
@MarcoGuignard That's exactly what the net40 framework is for. It lets you write libraries and applications that work on both .Net Core and .Net Framework, while taking advantage of .Net Framework-specific features, when you can.Fireproof
F
20

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.

Fireproof answered 20/7, 2016 at 1:29 Comment(3)
how to do the same in the new config file format .csproj ?Zohar
@Zohar do you know any example in .csproj?Trost
@Zohar check out this answer linkHotchkiss
R
65

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
Radiopaque answered 10/3, 2020 at 16:1 Comment(2)
Thank you. Using Windows Forms without WPF also required me to set <Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">.Bindman
I needed windows target platform to make this work e.g. <TargetFramework>net6.0-windows</TargetFramework>. Otherwise building gives the error "The target platform must be set to windows"Sawyer
B
57

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.

Bessie answered 29/11, 2021 at 8:49 Comment(4)
you only need to add -windows to the end of TargetFramework Value/Content. UseWPF and UseWindowsForms isnt quite necessary.Lamphere
@Lamphere you are wrong, once there is a call to anything "using" system.windows.form, it will fail at runtime if you don't put the UserWindowsForms COMBINED WITH -windowsNorfolk
@ChristopheChenel I see. Guess i learn something new every day lmaoLamphere
I can confirm that the following also works for >net 8: <PropertyGroup> <TargetFramework>net8.0-windows</TargetFramework> <UseWPF>true</UseWPF> <UseWindowsForms>true</UseWindowsForms> </PropertyGroup>Mcalister
F
20

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.

Fireproof answered 20/7, 2016 at 1:29 Comment(3)
how to do the same in the new config file format .csproj ?Zohar
@Zohar do you know any example in .csproj?Trost
@Zohar check out this answer linkHotchkiss
N
1

Note: the bellow was for .NET Core < 3, which came without WinForms on Windows.

However, it is still valid if you need to compile something with WinForms on Linux, since .NET Core WinForms only runs on Windows.

Mixing frameworks is certainly one way to go - but then, why do you use .NET Core ?

But what you can do is port the mono implementation of System.Windows.Forms to NetStandard.
Such as here: https://github.com/ststeiger/System.CoreFX.Forms

Nocturne answered 28/8, 2018 at 7:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.