How to make command not disable button when clicked

335 views Asked by At

I'm currently developping an application in MVVM. I'm using for all buttons RelayCommand to perform action. The fact is that, with RelayCommand, the button is disabled when clicked on it, the time that the command is executing. Because our rendering engine is a little bit heavy, when we open a new window, the button stay disabled for a bit, like a second.

My question is : is there a way to disable this behaviour and let the button enabled, because it changes style ? Changing disabled style is not an option ...

I haven't see that kind of behavior on the net or in the documentation ?

Thank you

EDIT :

Two differents implementations of my RelayCommand

        AnswerCommand = new RelayCommand(p => AnswerMail(p), p => IsMailSelected());
        DeleteMailCommand = new RelayCommand(p => Task.Run(() => DeleteMail()), p => IsMailSelected());
1

There are 1 answers

3
Olaru Mircea On

I think you should synchronize the access for the block existing in the ICommand instance Execute method and every time you get there, start a new thread. If you want to click on that Button many times, having that lock section there, will put your calls in a queue. If you want to reject the requests received while it's still working you can implement the balking pattern.

https://en.wikipedia.org/wiki/Balking_pattern

This way, your button will stop being disabled when the created thread is starting.

I am not sure if this approach helps you but i think it's a feasible scenario.

One more thing, i've just found this:

Patterns for Asynchronous MVVM Applications: Commands

i haven't used it before but looks nice, i will give it a try tonight. Good luck!