In my application I bound my buttons to commands with "MVVM". My command is implemented as follows:
public Command CommandLoadStuff
{
get
{
return new Command(async () =>
{
await DoLongStuff();
});
}
}
The problem is that these commands are async and the user can click them multiple times causing the code to execute multiple times also.
As a first approach i used CanExecute:
public Command CommandLoadStuff
{
get
{
return new Command(async () =>
{
AppIsBusy = true;
await DoLongStuff();
AppIsBusy = false;
},() => !AppIsBusy);
}
}
Now I wonder if there isn't a better way than to handle the CanExecute for each command individually.
Since I initialize the command every time with "new" I wonder if the class "Command" could not be extended accordingly. It should block a second click of the button during the lifespan with CanExecute (Posibly in the Constructor?) and release it after the execution of the command is finished. ( Possibly in the Dispose function?)
Is there a way to achieve this?