Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running Forms forms F# script in .net 5?

Is it possible to call Windows.Forms as F# script? As I am aware, there is an issue when trying to refer them a .nuget package, as described here, with potential solution (referencing the files directly on the target machine), described here. I did not try to see if suggested solution works with .net core 3.1 (which the post author used author used). However, I could not run the solution under .net 5, ever after referencing the files directly on the disc, with the error being something 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)".

Here is the code:

#r "DotnetLocation/dotnet/shared/Microsoft.WindowsDesktop.App/5.0.1/System.Windows.Forms"
#r "DotnetLocation/dotnet/shared/Microsoft.WindowsDesktop.App/5.0.1/System.Drawing"
#r "DotnetLocation/dotnet/shared/Microsoft.WindowsDesktop.App/5.0.1/System.Drawing.Common"
#r "D:/F# Examples/Chapter7Libs/Vector/bin/Debug/netcoreapp5.0/Chapter7libs"
open System.Drawing
open System.Windows.Forms
Application.EnableVisualStyles()let winSize = Size(450, 300)
let display (title: string, (c: Curve.Curve, pw: int, ph: int)) =
   let f(x,y) = Point(int(round x), ph - int(round y))
   let clst = Curve.toList c
   let ptLst = List.map f clst
   let pArr = Array.ofList ptLst    let pen = new Pen(Color.Black)
   let draw(g: Graphics) = g.DrawLines(pen, pArr)    let panel = new Panel(Dock=DockStyle.Fill)
   panel.Paint.Add(fun e -> draw(e.Graphics))    let win = new Form(Text=title, Size=winSize, AutoScroll=true, AutoScrollMinSize=Size(pw,ph))
win.Controls.Add(panel)
win.Show()

(The example if from "Functional programming using F#", chapter 7). Attempting to wrap Forms calls into a dll and call them from a script did not work either - the script just could not load the module that wrapped the calls (other modules from the same dll worked just fine).

PS: There is no problem when trying to compile the code, it works fine, my problem is only with interactive scripts.

like image 350
Raziel Magius Avatar asked Dec 19 '20 19:12

Raziel Magius


1 Answers

Citing Phillip Carter's comment to the question as the accepted answer, for now.

No, it's not a supported scenario at the moment. Winforms is not just a library anymore, it is its own special kind of dependency closure that is only currently understood by msbuild when it does a build. That's why it works in a project but not in a script

like image 100
Raziel Magius Avatar answered Oct 23 '22 18:10

Raziel Magius