Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I cannot tell you exactly what events the <code>CommandManager</code> listens to. However, I can tell you that you should <strong>be careful when using the <code>CommandManager</code> in connection with asynchronous operations</strong>. I had the following problem when I used the <code>CommandManager</code> in my <code>ICommand</code> implementations:</p> <p>I had a button bound to an <code>ICommand</code> which triggered an asynchronous operation which increased a value. Now, the button/<code>ICommand</code> should be disabled (i.e. its <code>CanExecute()</code> method should return <code>false</code>) if the value had reached a certain limit. The problem was: The <code>CommandManager</code> called my <code>CanExecute()</code> method right after the button had been clicked and the asynchronous operation had been started. This asynchronous operation did not take long, but it was long enough to get its result <em>after</em> the <code>CommandManager</code>'s check, so that the limit check in <code>CanExecute()</code> was done using the old value. Therefore, the button remained enabled although the limit was actually reached. The funny thing was, after you clicked anywhere in the UI, the button now got disabled because the <code>CommandManager</code> checked the <code>ICommand</code> once again and now the new value was checked against the limit. Actually, I think the <code>CommandManager</code> waited around 50ms after the button click until it performed the check of the <code>ICommand</code>, but I am not quite sure about that.</p> <p>My solution was to force the <code>CommandManager</code> to check the <code>ICommand</code> again by calling the <a href="http://msdn.microsoft.com/en-us/library/system.windows.input.commandmanager.invalidaterequerysuggested.aspx" rel="noreferrer"><code>CommandManager.InvalidateRequerySuggested</code></a> method in my ViewModel right after I received the result of the async operation. <strong>Update:</strong> Please note that this method must be called on the UI thread, otherwise it will have no effect! <em>(Thanks to midspace for this comment)</em></p>
 

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