MVVM Light: how to unregister Messenger

后端 未结 3 772
夕颜
夕颜 2021-01-30 08:40

I love the MVVM Light\'s Messenger and its flexibility, however I\'m experiencing memory leaks when I forget to explicitly unregister the recipients (in Silverlight 4).

3条回答
  •  北荒
    北荒 (楼主)
    2021-01-30 09:19

    The ViewModelLocator class helps keep a centralized store for your viewmodels. You can use this class to help manage new versions and clean up old ones. I always reference my viewmodel from view via the locator, so I always have code I can run to manage these things. You could try that.

    As well, I use the Cleanup method to call Messenger.Unregister(this), which cleans up all references from the messenger for that object. You have to call .Cleanup() every time, but such is life :)

提交回复
热议问题