<...">

How to save a button during an asynchronous task

In my xaml I have a button and a text block

 <TextBlock Text="{Binding AText}" FontSize="20"/> <Button Content="Click" Command="{Binding MyCommand}" Grid.Row="1"/> 

and in my ViewModel I have code

 let aText = self.Factory.Backing(<@ self.AText @>, "Initial Text") let asyncTask x = async { self.AText <- "Loading" do! Async.Sleep(5000) self.AText <- "Loaded" } member self.AText with get() = aText.Value and set(v) = aText.Value <- v member self.MyCommand = self.Factory.CommandAsyncChecked(asyncTask, fun _ -> true) 

When I click the button, it shuts off and stays that way until it finishes asyncTask . I thought setting canExecute to true would change the behavior, but it is not!

How to control button behavior?

+5
source share
1 answer

Support for the Async command in FSharp.ViewModule was specifically designed to disable commands during operation to prevent them from reappearing.

If you do not want this behavior, the easiest option is to simply use the regular command and start the async workflow manually:

 let asyncTask () = async { self.AText <- "Loading" do! Async.Sleep(5000) self.AText <- "Loaded" } |> Async.Start member self.MyCommand = self.Factory.Command asyncTask 

Normal team behavior will always remain on.

+6
source

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


All Articles