5

In my xaml I have a Button and a TextBlock

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

and in my ViewModel I have following 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 gets disabled and remains so until it finishes the asyncTask. I thought that setting canExecute as true will change the behavior but it did not!

How to control the behaviour of the button?

FoggyFinder
  • 2,230
  • 2
  • 20
  • 34
  • What you want to do? Because the current behavior is the most logical. – FoggyFinder Nov 29 '16 at 20:05
  • @FoggyFinder ofcourse it is. As I have the option to pass `canExecute` as an argument to `CommandAsyncChecked` so the button should do what `canExecute` says. I think my argument in this case is more logical. Do you agree? –  Nov 29 '16 at 20:13
  • 1
    Yes, it does make some sense. – FoggyFinder Nov 29 '16 at 20:23
  • 3
    EmonHaque and @FoggyFinder The main difference between the async and normal commands is really this behavior - the async commands use CanExecute _and_ whether there's an async operation in flight to determine whether to enable. – Reed Copsey Nov 29 '16 at 21:10

1 Answers1

5

The Async command support in FSharp.ViewModule was designed specifically so that the commands disable while operating, in order to prevent them from occurring multiple times.

If you don't want that behavior, the simplest option is to just use a normal 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

The normal command behavior will always remain enabled.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • It works. One more question - how can I edit the `Xaml` in blend? I have tried to open the project in blend but it says "Unsupported ....................." –  Nov 29 '16 at 20:31
  • @EmonHaque I don't think Blend will support it without making a separate C# project that links/includes the xaml files - Blend is pretty limited in that regard. I typically just hand edit the xaml files. – Reed Copsey Nov 29 '16 at 20:33
  • Is there any reporting tool like `CrystalReport` or `XtraReport` that works in `FsXaml` project? –  Nov 29 '16 at 21:07
  • 1
    @EmonHaque Any WPF based one should work - but I've never tried them ;) – Reed Copsey Nov 29 '16 at 21:08
  • `MaterialDesign XAML` works fine with `FsXaml`. But none of those reports works probably because both of them adds `.cs` file backing the report whenever I add any of them in a C# WPF project. –  Nov 30 '16 at 12:22