winforms – 在F#中使用Windows窗体的面向对象的“Hello world”
发布时间:2020-12-14 01:50:20 所属栏目:Windows 来源:网络整理
导读:我目前正在通过“真实世界的功能编程”.我试图让1.12工作,一个使用 Windows窗体的“hello world”程序.这是代码: – open System.Drawing;; open System.Windows.Forms;; type HelloWindow() = let frm = new Form(Width = 400,Height = 140) let fnt = new
我目前正在通过“真实世界的功能编程”.我试图让1.12工作,一个使用
Windows窗体的“hello world”程序.这是代码: –
open System.Drawing;; open System.Windows.Forms;; type HelloWindow() = let frm = new Form(Width = 400,Height = 140) let fnt = new Font("Times New Roman",28.0f) let lbl = new Label(Dock = DockStyle.Fill,Font = fnt,TextAlign = ContentAlignment.MiddleCenter) do frm.Controls.Add(lbl) member x.SayHello(name) = let msg = "Hello" + name + "!" lbl.Text <- msg member x.Run() = Application.Run(frm);; let hello = new HelloWindow();; hello.SayHello("you");; hello.Run();; 不幸的是,这会引发错误 – “在单个线程上启动第二个消息循环不是有效的操作.”显然,有一个窗口打开而不是终止,这使程序混乱.我看不出如何修复错误,任何人都可以帮助我吗? 我也尝试输入最终的代码块: – let hello = new HelloWindow() hello.SayHello("you") hello.Run();; 但这没有用.代码运行正常但没有结果,最后一行被注释掉了.
该示例旨在编译并作为Windows窗体应用程序运行.如果您想在F#Interactive中运行它,则必须使用frm.Show()而不是Application.Run(frm).
您可以使用compiler directives在F#Interactive和编译项目中使示例工作: type HelloWindow() = let frm = new Form(Width = 400,Height = 140) // ... // The same as before member x.Run() = #if INTERACTIVE frm.Show() #else Application.Run(frm) #endif (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- windows – WipeDrive实用程序?
- windows – 如何证明操作系统级防火墙的重要性?
- windows-xp – 将Windows休眠文件移动到其他驱动器
- windows-store-apps – 如何将Windows应用商店8.1应用移植到
- wpf – 在Windows经典主题中更改禁用列表框的背景颜色
- 在Windows命令中处理扩展字符?
- windows – 在没有窗口的计划任务中运行.bat文件
- windows-mobile – 如何在紧凑框架中获取当前目录?
- Windows Phone 7 – 如何在LongListSelector中包含ItemsPan
- windows-mobile – 使Windows Mobile设备模拟蓝牙HID设备