Close Wpf Application in FSharp Interactive

106 views Asked by At

I'm currently trying some F# programming together with WPF. I'm trying to make use of FSI as much as it's possible, but have no idea how to work with it if I want to run some WPF windows.

After creating a window with simple

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

if I close the window (with the x), next time I'm trying to (alt+Enter) app.Run(myWindow) I get an error saying that I cannot run more than one System.Windows.Application in the same AppDomain. Should I close the window in a different way? I was trying to use app.Shutdown() after closing the window but it gives an error saying tat app is not defined.

When the window is still open FSI is unresponsive.

Second question - Is there a way to somehow operate with the opened window? I mean add some controls or modify existing ones in runtime from FSI?

Sample.fsx which can be run from FSI to open the WPF window is below:

#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

There are 0 answers