WPF TextBox / RichTextBox started with F # Interactive not accepting input

Getting this strange problem, not sure if it's just me, and I don't remember it happening before ...

#r "PresentationFramework"
#r "PresentationCore"
#r "WindowsBase"
System.Windows.Window(
    Content = System.Windows.Controls.TextBox())
    .Show()

A window will appear, a message will appear TextBox, but I cannot enter alphanumeric text if I do not insert them, but spaces. The same goes for RichTextBox.

On the other hand, it works fine withShowDialog() , which made me suspect that this might have something to do with the event pump, so I replaced fsi.EventLoop with WPF alone - but for some reason this call will not even call Run().

Edit: The “answer” on another question uses Application.Run()that, which is practically the same as using ShowDialog()that both block calls. However, this destroys the usefulness of calling WPF from the FSI. My question is about using TextBox controls without blocking a window call.

+4
source share
1 answer

Gotta love this sociable happy moderation.

Let me pick up this five-year post and raise you to six. All for code duplication, Jon Harrop solution to the table.

#r "PresentationFramework"
#r "PresentationCore"
#r "WindowsBase"

open System.Windows
open System.Threading
open System.Windows.Threading
open System.Windows.Controls

let ui =
    let mk() =
        let wh = new ManualResetEvent(false)
        let application = ref null
        let start() =
            let app = Application()
            application := app
            ignore(wh.Set())
            app.Run() |> ignore
        let thread = Thread start
        thread.IsBackground <- true
        thread.SetApartmentState ApartmentState.STA
        thread.Start()
        ignore(wh.WaitOne())
        !application, thread
    lazy(mk())

let spawn : ('a -> 'b) -> 'a -> 'b =
    fun f x ->
        let app, thread = ui.Force()
        let f _ =
            try
                let f_x = f x
                fun () -> f_x
            with e ->
                fun () -> raise e
        let t = app.Dispatcher.Invoke(DispatcherPriority.Send, System.Func<_, _>(f), null)
        (t :?> unit -> 'b)()

let myWindow() =
    let wnd = Window(Content = TextBox())
    wnd.Show()
    wnd

spawn myWindow ()
0
source

Source: https://habr.com/ru/post/1695731/


All Articles