I like MVVM Light Messenger and its flexibility, but I experience a memory leak when I forget to explicitly unregister recipients (in Silverlight 4).
The reason is explained here , but I'm fine, as I find it good practice to explicitly unregister recipients, rather than relying on the use of weak links in Messenger. The problem is that it's easier said than done.
ViewModels are simple: you usually have full control over your life cycle and you can just Cleanup() them when they are no longer needed.
Views , on the other hand, are more complex because they are created and destroyed using DataTemplates. E.g. you can think of ItemsControl with MyView as a DataTemplate bound to an ObservableCollection<MyViewModel> . MyView are created / built by the binding mechanism, and you have no good way to manually call Cleanup () on them.
I have a solution, but I would like to know if he has a decent sample or if there are better alternatives. The idea is to send a specific message from the ViewModel to inform the related View (s):
public class MyViewModel : ViewModelBase { ... public override void Cleanup() {
Therefore, when you call Cleanup () on the viewModel, all views that use it as a DataContext will also do their local cleanup ().
What do you think? Am I missing something?
Francesco De Vittori Mar 10 '11 at 8:12 2011-03-10 08:12
source share