How can I use Windows Form in a dotnet core Fsharp script?

How can I used Windows Form in a dotnet core Fsharp script?

open System.Windows.Forms

raise the warning or error that The namespace ‘Forms’ is not defined.

I understand that using Windows.Forms in dotnet need some work or special setup
and there is a link for this https://docs.microsoft.com/en-us/dotnet/core/porting/winforms

But I couldnt find a way to declare this for an F# script

1 Like

You will have to reference the DLLs providing the Windows Forms API.

One thing you could try is to run fsi.exe with the --langversion:preview switch, add a #r "nuget: System.Windows.Forms" to the top of your script, and keep your fingers crossed that the open System.Windows.Forms statement and further use of the API works :wink:

Another option would be to use Paket and generate a load script for the package that you could reference from your own script: Change to a fresh working directory, initialize Paket using paket init, add a reference to the Windows Forms package with paket add System.Windows.Forms, generate a load script with paket generate-load-scripts -f netcore3.1 -t fsx, add a #load ".paket/load/netcoreapp3.1/System.Windows.Forms.fsx" to the top of your script, and again hope for the best.

2 Likes

First thanks for the reply and suggestions

I tried both options, I am getting the same error

error FS0193: internal error: Could not load file or assembly ‘System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL’. Reference assemblies should not be loaded for execution. They can only be loaded in the Reflection-only loader context. (0x80131058)

What does this mean :thinking:

It seems the Windows Forms stuff is handled specially – the NuGet package is actually unlisted, contains only stub libraries apparently, and regular .fsproj files do not reference the package directly but rather set a magic flag <UseWindowsForms>true</UseWindowsForms> to enable Windows Forms support for a netcoreapp3.* target. I have no idea how to emulate this for an F# script :confused:

Thanks :slight_smile:

I also notice, the dotnet command does support or doesnt have an F# winforms template
But I think I can start with a console app and add it as package

I will start from there

Ok, I cant find how to create an winforms app in F# in dotnet core
Is this at all possible?
Why is it so complicated? why winforms isnt just another package or library that you can add, reference and then use?

I am asking, because I think it will help me understand the platform better, winforms seem special and it even seem to have special flags in the project file

<UseWindowsForms>true</UseWindowsForms>

Why is all of this needed?
And finally how can I do it in an F# template although dotnet core doesnt have a template for it

The assemblies are actually included in the .NET Core SDK, rather than via NuGet. Same with ASP.NET Core, actually. On my machine they’re located in the folder C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App\3.1.4\. I haven’t tried to reference them from fsi though, not sure if it’s enough.

Ok so I finally got listing 4.8 from Get Programming with F#

My project file look like this

> <Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
> 
>   <PropertyGroup>
>     <OutputType>WinExe</OutputType>
>     <TargetFramework>netcoreapp3.1</TargetFramework>
>     <UseWindowsForms>true</UseWindowsForms>
>   </PropertyGroup>
>
>   <ItemGroup>
>     <Compile Include="Program.fs" />
>   </ItemGroup>
> </Project>

I think the key line was specifying the correct Sdk
As I am a beginner in F#, I still dont know much abut the diffeent SDKs why they exists , and which one to use one etc …

I also dont know why this entry was needed, what does it do
<UseWindowsForms>true</UseWindowsForms>