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

后端 未结 7 512
南笙
南笙 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-30 18:39

    is there a way I can make CommandManager.InvalidateRequerySuggested() do it's job faster?

    Yes, there is way to make it work faster!

    1. Implement Command to keep / cache CanExecuteState in a boolean variable.
    2. Implement RaiseCanExecuteChanged method to recalculate CanExecuteState and if it really changed to raise CanExecuteChanged event.
    3. Implement CanExecute method to simply return CanExecuteState.
    4. When InvalidateRequerySuggested method is invoked Command subscribers will only read CanExecuteState variable by invoking CanExecute method and check if it changed or not. That's almost zero overhead. All Commands will be disabled / enabled almost the same time.
    5. All work will be done in RaiseCanExecuteChanged method that will be called only once for a Command and only for a limited set of Commands.

提交回复
热议问题