-1

How to block UI in Caliburn.Micro?

public async void AcceptButton()
{
    await this.httpDataService.Register(account);
    Show.SuccesBox(alert);
    this.TryClose();
}

How to wait for end of Task in my ViewModel by blocking View?

EDIT

i added binding on my button in xaml:

 IsEnabled="{Binding isEnabled}"

Then, my VM:

bool isEnabled {get;set;}

public async void AcceptButton()
{
    this.isEnabled = false;
    await this.httpDataService.Register(account);
    Show.SuccesBox(alert);
    this.TryClose();
}

In this case, AcceptButton is always unactive IsEnabled=false. How to trigger false only on button click?

michasaucer
  • 4,562
  • 9
  • 40
  • 91

1 Answers1

0

because i dont want to user double-tap "Send form" button

The standard way of doing that is just disabling the button. Or, if you want to disable the entire form, then disable the entire window:

public async void AcceptButton()
{
  this.IsEnabled = false;
  await this.httpDataService.Register(account);
  Show.SuccesBox(alert);
  this.TryClose();
}

You should add the IsEnabled property to your view model class and then bind it to the IsEnabled property of the view.

Disabling the window is nicer to the user than an unresponsive application, because this approach allows the user to minimize it or move it out of the way if it is taking an unexpectedly long time.

mm8
  • 163,881
  • 10
  • 57
  • 88
Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810
  • Thanks for your feedback, but i mentioned `Caliburn.Micro` in my thread because im using this framework for my views. Because of that, i cant block my button as you mention, because `IsEnabled` is undefined in caliburn;s `ViewModels` – michasaucer May 02 '19 at 19:02
  • @michasaucer: You should add the `IsEnabled` property yourself to your view model class and then bind it to the `IsEnabled` property of the view. Don't fortget to set it back to true once the task has completed. – mm8 May 03 '19 at 09:09
  • Thanks! I added `Binding = ButtonEnableState` to my button on `IsEnabled`, i make property in `VM` class and it works! Please edit your answer so you will help someone in future :) – michasaucer May 03 '19 at 09:13
  • I have a little problem, i make property like yours (`IsEnabled` with `get, set`), i set it to false on `AcceptButton` but on window launch, it is set to `false` (before i clicked on my button). Can you tell me, how to trigger `false` ONLY when i click the button, not before? – michasaucer May 03 '19 at 09:16
  • @StephenCleary could you look on my `edit` in original post? – michasaucer May 03 '19 at 09:19
  • 1
    @michasaucer: Set the property to `true` by default: `bool isEnabled {get;set;} = true;` – mm8 May 03 '19 at 09:26
  • @mm8 now i can click on button (and it works) but its not change to `false` when i click on `AcceptButton` (its stays on `true`) – michasaucer May 03 '19 at 09:28
  • @michasaucer: You need to raise the `PropertyChanged` event for the property after you've set it: `this.NotifyOfPropertyChange( () => this.IsEnabled );` – mm8 May 03 '19 at 09:28