As stated in msdn in the notes here , CommandManager.RequerySuggested contains only a weak reference to the event. In your unit test, the lambda garbage expression is compiled.
Try the following:
bool triggered; EventHandler handler = (s, e) => triggered = true; CommandManager.RequerySuggested += handler; CommandManager.InvalidateRequerySuggested(); GC.KeepAlive(handler); Assert.IsTrue(triggered);
Update
From some further research, I believe that I have identified the problem.
CommandManager.InvalidateRequestSuggested() uses the current dispatcher for an asynchronous asynchronous event.
Here is the solution:
bool triggered; EventHandler handler = (s, e) => triggered = true; CommandManager.RequerySuggested += handler; CommandManager.InvalidateRequerySuggested(); // Ensure the invalidate is processed Dispatcher.CurrentDispatcher.Invoke(DispatcherPriority.Background, new Action(() => { })); GC.KeepAlive(handler); Assert.IsTrue(triggered);
source share