0

Im implementing a simple Button in WPF, that is bound in XAML to an ICommand.

Whilst it works great, I've got it triggering CanExecute() via the following code:

 public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }

The problem is, the instantiated ICommand is being kept alive by this event, and even after I've finished with it, CanExecute() is continually triggered by the CommandManager.

I supposed I've got a temporary undesirable fix in the form of a List<EventHandler> inside the ICommand instance, that adds each incoming value from the add{} , which i'm then manually unsubscribing from by just iterating it and calling CommandManager.RequerySuggested -= eventItem, but I'm wondering if there is a better way or i'm using it wrongly?

maxp
  • 24,209
  • 39
  • 123
  • 201
  • Please read here - http://stackoverflow.com/a/3047677/1979354 – Spawn Sep 29 '15 at 19:13
  • That seems to indicate it should be garbage collected, but then a load of users report that it isn't, and `CanExecute` is still being called - which i'm also facing... – maxp Sep 29 '15 at 19:22
  • 1
    Did you check it on release without debugger? – Spawn Sep 29 '15 at 19:24
  • Argh, so it works correctly without the debugger, but this is not good enough unfortunately :(. – maxp Sep 29 '15 at 19:35
  • Why this is not good? Debugger is pretty great feature of Studio, but, yes, it works little strange with disposing. – Spawn Sep 29 '15 at 19:38
  • https://www.youtube.com/watch?v=EpGvqVtSYjs&list=PLcJBKdjm-X2QuY5CxC75TftE_Rfwi3BAP&index=2&t=710s – Iria Mar 13 '20 at 14:44

0 Answers0