I am using Josh Smith's MVVM Foundation project Messenger class and 开发者_如何学Cjust had a question regarding unsubscribing messages. At the moment I have multiple ViewModels which register messages as strings for example I have -
ViewModelA - registers for message "A" ViewModelB - registers for message "A"
So the both registered messages have the same string.
My question, what is the best way to unregister a specific message from an instance of a ViewModel when that ViewModel is closed (given that multiple ViewModels can have the same message string).
Cheers,
Tim.
The whole key to the Messenger class is that it holds only weak references to subscribers ("colleagues"), so if you close your ViewModel, it will cease receiving messages as soon as it is garbage collected.
If waiting for garbage collection still isn't good enough, you could try forcing garbage collection. See this link for more information.
The only other alternative I can think of is to subclass Messenger
and write your own Unregister()
method.
精彩评论