PropertyObserver doing foreach handler invoke N number of times


One of the PropertyObserver feature I like is that by calling RaisePropertyChanged(null) or RaisePropertyChanged(string.Empty) on the source object, it will loop through all the registered handlers and invoke them one-by-one.

But during creation of a unit test for this functionality I found out that the ReceiveWeakEvent() method is called N number of times with null/string.Empty as property name, with N as the number of registered handlers. So the foreach loop will be done N number of times, which I think is not the intended behavior.

Do you have any idea on how to fix this issue?


pogosama wrote Nov 7, 2015 at 10:51 AM

Okay I found the bug: When calling RegisterHandler(), each time PropertyChangedEventManager.AddListener() is called again. This is incorrect, because we only need to call this once for every unique listener (in this case the PropertyObserver class) by specifying string.Empty in the third argument.

See: https://msdn.microsoft.com/en-us/library/system.componentmodel.propertychangedeventmanager.addlistener(v=vs.110).aspx

When all handlers are unregistered we can then call PropertyChangedEventManager.RemoveListener(), also with string.Empty as third argument.