I am testing out making a UWP app using the Fabulous framework for writing functional cross-platform apps, and I want to use a FilePicker on a button press and use the selected file for some data processing.
Executing
let fileResult = FilePicker.PickAsync() |> Async.AwaitTask
opens the file picker and returns a Async<FileResult>
after a file is picked (This to say that the button and subsequent function call executes), but the rest of the code following it will execute before the result can be used. If I append |> Async.RunSynchronously
it (as expected) blocks the thread and no file can be chosen in the window that appears, although the return value would be the FileResult.
After looking into how this should be done, I realise that the file picker should be opened on the main thread, which leads me to a solution on the following form
let getFileResultAsync =
async {
let tcs = new TaskCompletionSource<FileResult>()
Device.BeginInvokeOnMainThread(fun () ->
async {
let! fileResult = FilePicker.PickAsync() |> Async.AwaitTask
tcs.SetResult(fileResult)
}
|> Async.StartImmediate
)
return! tcs.Task |> Async.AwaitTask
}
which will return Async<FileResult>
, but it appears that the Device.BeginInvokeOnMainThread block is never accessed. How would I go about opening the FilePicker, selecting a file and then subsequently process the file in such an app?