Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your CanExecute shouldn't be so long and complex that this would actually be an issue.</p> <p>Short of checking the CanExecute again at the moment of running the Execute (which will happen synchronously provided you are not doing bad things in the CanExecute) then there is little more you can do. Your business rules should catch business related issues on the way to the data repository, and any other functions that are called as a result of the Execute can also do their own validation and data checking.</p> <p><i><strong>If</strong></i> this is happening then to me that means you have a problem somewhere in the CanExecute. Or possibly the CanExecute wasn't called (maybe a property changed and the command wasn't re-evaluated).</p> <p>Unless you give a solid example of this misbehaving code then that is the most I can say.</p> <h2>Edit:</h2> <blockquote> <p>Calling CanExecute in Execute method is not a problem, but what to do if we've got false? Do we need just to exit?</p> </blockquote> <p>Yes, you can just exit. Alternatively you could throw a validation related exception (not really recommended because you'll usually be calling this via binding). Basically the Execute is the start of the chain, anyone or anything calling it should be set up to handle errors - you should never expect success simply because it was called. </p> <blockquote> <p>But what if my colleague have created a component that uses my command and forgot to check CanExecute before calling Execute?</p> </blockquote> <p>This is a case where I would say it is bad to have the command implementation in the view model and then bind to it. If the command was instead implemented in the code behind of the view, you can call from there through to the view model to do whatever work is required (like save some data), and wrap that call in a try/catch, if you catch an exception you can then easily display something in the UI (this is not possible from the VM if you are correctly adhering to MVVM).</p> <p>Just remember: binding to commands is great, but nobody said the commands all had to be in the viewmodel. Even the CanExecute can call through to the viewmodel so that data validation can be done - if you are being a good coder then all the methods/properties on your viewmodel that are required by the view are exposed via an interface.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload