在FSharp Interactive中关闭Wpf应用程序

时间:2018-11-19 00:13:15

标签: wpf f# fsi

我目前正在尝试与WPF一起进行一些F#编程。 我正在尝试尽可能多地使用FSI,但不知道如果要运行某些WPF窗口,该如何使用它。

使用简单的窗口创建

let myWindow = Window()
let app = Application()
app.Run(myWindow)

如果我关闭窗口(带有x),则下次尝试(alt + Enter)app.Run(myWindow) 我收到一条错误消息,说我在同一AppDomain中不能运行多个System.Windows.Application。我应该以其他方式关闭窗户吗?关闭窗口后,我试图使用app.Shutdown(),但是它给出了一个错误消息,提示未定义tat应用程序。

当窗口仍然打开时,FSI没有响应。

第二个问题-是否可以通过某种方式对打开的窗口进行操作?我的意思是在运行时通过FSI添加一些控件或修改现有控件?

可以从FSI运行以打开WPF窗口的Sample.fsx如下:

#I @"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5"
#r "PresentationCore.dll"
#r "PresentationFramework.dll"
#r "System.Xaml.dll"
#r "WindowsBase.dll"

open System.Windows
open System.Windows.Controls

let buttonNames = [
    "First button"
    "Second button"
    "Third button" ]

type StackPanel with
    member stack.add x = stack.Children.Add x |> ignore

let createSmallButton text = 
    let myButton = Button(Content=text)
    myButton.Height <- 40.
    myButton.Width <- 100.
    myButton

let myStack = StackPanel()

let buttons = 
    buttonNames 
    |> List.map createSmallButton 

buttons |> List.iter myStack.add

let myWindow = Window()
myWindow.Content <- myStack

let app = Application()
app.Run(myWindow)

0 个答案:

没有答案
相关问题