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
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.
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)
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
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.
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>
@Palmer
When trying use this solution from .net 5 - after referencing the relevant dlls, I end up with someting like " “Could not load file or assembly ‘System.Drawing.Common, Version=5.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51’. Could not find or load a specific file. (0x80131621)”.
Can you confirm that it works under .net 5? Did it work under .net 3.1 at the end?
Thanks.