MVVM Light Messenger executing multiple times

后端 未结 5 1735
不知归路
不知归路 2021-01-13 11:29

I am using MVVM Light and am using Messages to communicate between ViewModels to let a ViewModel know when it is ok to execute something. My problem is that I register for

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-13 12:08

    Make sure you unregister your message handlers once you do not need them anymore. The Messenger keeps a reference to the registered methods and this prevents them from being garbage collected.

    Therefore, for ViewModels: make sure that you call Cleanup once you done (or implement IDisposable and call Cleanup from there).

    For Views (Controls, Windows, or similar) call Messenger.Unregister in an event that occurs on the teardown of the view, e.g. the Unloaded event.

    This is a known behaviour of the MVVM and has been discussed in several places.

提交回复
热议问题