MVVM Light Listener does not release / deterministic finalization for registered object?

I have a child branch with a linked virtual machine that is created every time I ask to open a child window. When a child branch opens, it registers a listener for the MVVM Light message. After I close the window, I am sure that I will release all references to it, but in fact I do not call dispose, because it does not implement IDisposeable.

When I initialize another child window of the same type and send it a different context, I know that I get a message from the previous instanciation of the virtual machine ... every time I use the window, more and more VMs listen and the code repeats.

How can I be sure that my previous virtual machine registered to listen to the message was really released and is no longer active. Is there a deterministic way to do this?

+4
source share
3 answers

Whenever you register a message, you must ensure that you also unregistered the message. To unregister, you can use the method Cleanupfor classes derived from ViewModelBase. In other cases, for example, a view, you must implement a method that is called when the view is unloaded, for example, by capturing and processing the unloaded event on a control or view. In this method you call Messenger.Unregister(EventTarget).

, .

+2

? , , , , . , Messenger (. ). : IDisposable , . , -.

+1

You can use the cleanup method or manually unregister the message. Click here for more details .

0
source

All Articles