首页 > 解决方案 > 在 .net 5 中运行 Forms 表单 F# 脚本?

问题描述

是否可以将 Windows.Forms 称为 F# 脚本?据我所知,在尝试将它们引用为 .nuget 包时存在问题,如此处所述潜在的解决方案(直接在目标机器上引用文件),如此所述。我没有尝试查看建议的解决方案是否适用于 .net core 3.1(帖子作者使用作者使用的)。但是,在直接引用光盘上的文件之后,我无法在 .net 5 下运行解决方案,错误类似于“无法加载文件或程序集‘System.Drawing.Common, Version=5.0.0.0, Culture =中性,PublicKeyToken=cc7b13ffcd2ddd51'。找不到或加载特定文件。(0x80131621)“。

这是代码:

#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()

(示例来自“使用 F# 的函数式编程”,第 7 章)。尝试将 Forms 调用包装到 dll 中并从脚本中调用它们也不起作用 - 脚本只是无法加载包装调用的模块(来自同一 dll 的其他模块工作得很好)。

PS:尝试编译代码时没有问题,它工作正常,我的问题只是交互式脚本。

标签: f#f#-interactive.net-5

解决方案


暂时引用Phillip Carter对该问题的评论作为已接受的答案。

不,目前这不是受支持的方案。Winforms 不再只是一个库,它是它自己的特殊依赖闭包,目前只有 msbuild 在构建时才能理解。这就是为什么它在项目中有效但在脚本中无效的原因


推荐阅读