MVVM Light: how to unregister Messenger

后端 未结 3 768
夕颜
夕颜 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 :)

    0 讨论(0)
  • 2021-01-30 09:25

    I've not used MVVM Light (though I've heard great things), but if you want a Messenger implementation that uses WeakReferences, checkout the Messenger included here http://mvvmfoundation.codeplex.com/.

    0 讨论(0)
  • 2021-01-30 09:40

    MVVM Light Messenger is using WeakAction(WeakReference). So you don't need to Unregister explicitly.

    0 讨论(0)
提交回复
热议问题