CommandManager.InvalidateRequerySuggested() isn't fast enough. What can I do?

后端 未结 7 501
南笙
南笙 2020-11-30 18:09

Short Version

Calls to CommandManager.InvalidateRequerySuggested() take far longer to take effect than I would like (1-2 second delay

7条回答
  •  南方客
    南方客 (楼主)
    2020-11-30 18:22

    Tomas has a nice solution, but pls note there's a serious bug in that the CanExecute will not always fire when bound to a Button due to this :

    // Call the handlers that we snapshotted
    for (int i = 0; i < count; i++)
    {
                EventHandler handler = callees[i];
                handler(null, EventArgs.Empty);
    }
    

    The 'null' parameter passed in causes issues with the CanExecuteChangedEventManager (used by the WPF Button class to listen to changes on any Command bound to it). Specifically, the CanExecuteChangedEventManager maintains a collection of weak events that need to be invoked to determine if the command Can-Execute() but this collection is keyed by the 'sender'.

    The fix is simple and works for me - change the signature to

    internal static void CallWeakReferenceHandlers(ICommand sender, List handlers)
    {
    ....
               handler(sender, EventArgs.Empty);
     }
    

    Sorry I haven't described it too well - in a bit of a rush to catch up with my dev now after taking a few hours to figure this out !

提交回复
热议问题