-1

what is best practice to make a DelegateCommand from Prism framework in MVVM only one time executable in order to prevent click-spamming the button which may result in application crashes. many thanks!

Julian Peil
  • 105
  • 10
  • Possible duplicate of [RelayCommand change canExecute automatic](https://stackoverflow.com/questions/50927967/relaycommand-change-canexecute-automatic) – Haukinger Jun 23 '18 at 14:44

1 Answers1

0

Here is what I normally do:

  • You should have a property for what you are deleting. Use CanExecute
  • in your delegate command, watch the property if it is null? Use
  • ObserveProperty in your DelegateCommand and set it to What you are
  • deleting. In your DeleteCommandExecute set the property to null after deleting.

here is an example

    private Class object;
    public Class Object
    {
        get { return object; }
        set { SetProperty(ref object, value); }
    }
    private DelegateCommand _delete;
    public DelegateCommand Delete =>
        _delete ?? (_delete = new DelegateCommand(ExecuteDelete, CanExecuteDelete)).ObservesProperty(()=> Object);

    void ExecuteDelete()
    {
        MyServices.Delete(Object);
        Object = null;
    }

    bool CanExecuteDelete()
    {
        return Object != null;
    }
  • Wouldn't it be better to say like CanExecuteDelete = false after void ExecuteDelete() and instead of ObservesProperty using ObservesCanExecute(()=>CanExecuteDelete)? Ty for your answer! – Julian Peil Jun 25 '18 at 06:51
  • if you use CanExecuteDelete= false then you will not be able to click the command. The answer provided will check the canExecuteMethod only if the object raised a notification. That is why i set the object to null in the ExecuteCommand which will raise a notification which will cause the command to check the CanExecute. – Sulieman Mansouri Jun 26 '18 at 15:57