mvvm light icleanup和Messenger在VB.NET中注销 [英] mvvm light icleanup and messenger unregister in VB.NET

查看:360
本文介绍了mvvm light icleanup和Messenger在VB.NET中注销的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只需要一点点帮助来说明Messenger类如何与MVVM-Light中的ICleanup一起使用.我正在使用Mvvm-Light v4在VB.Net中创建WPF 4应用程序.

I just need a little help to clarify how the Messenger class works with ICleanup in MVVM-Light. I am creating a WPF 4 application in VB.Net with Mvvm-Light v4.

我有一个Messenger,用于注册从我按如下方式创建的NavigationService类发送的消息:

I have a messenger that registers for a message sent from a NavigationService Class I created as follows:

这是在ViewModel中名为ClientListViewModel的注册

This is the registration in the ViewModel named ClientListViewModel

 ''register for messages
  Messenger.[Default].Register(Of INavigationService)(Me, "NavigationStart", False, AddressOf HandleParentChildNavigate)

这将接收NavigationService类,并根据与该问题无关的其他逻辑对其进行检查.

This receives the NavigationService class and performs a check on it based on some other logic that is not relevant to this question.

在我的NavigationService类中触发导航事件时,将按以下方式发送消息

The message is sent when a navigation event is triggered in my NavigationService class as follows

''Send message that navigation has been requested
Messenger.Default.Send(Of INavigationService)(Me, "NavigationStart")

如果我的接收类(在本例中为ClientListViewModel)遇到数据验证错误,这使我可以取消导航事件,并且它将焦点返回错误的记录.这一切都很好.

This allows me to cancel the navigation event if my receiving class (in this case ClientListViewModel) has data validation errors, and it returns the focus to the record with errors. This all works great.

我的问题是,我在哪里以及如何注销邮件.我知道我需要避免在其他帖子上阅读时出现内存泄漏.我已经看到类似以下的内容:

My question is, where and how do I unregister the message. I know that I need to in order to avoid memory leaks as I read on other posts. I have seen something like the following:

Public Overrides Sub CleanUp()
    Messenger.Default.Unregister(Me)
End Sub

此清理在接收消息的同一视图模型(CientListViewModel)中.

This cleanup is in the same viewmodel (CientListViewModel) the receives the message.

所以我有三个问题:

  1. 何时应调用此清理方法

  1. When should I call this cleanup method

使用ViewModelLocator可以在应用程序关闭时注销所有消息收件人吗?

Is ther a way with the ViewModelLocator to unregister all message recipients when the application is closed down?

这个问题不太重要,但我也希望得到一些帮助,如何确定由于未注册的邮件接收者而导致的内存泄漏"?

This question is less relevant but I would appreciate some help too, How do I tell if I am getting "memory leaks" as a result of unregistered message recipients?

感谢您的时间

推荐答案

何时应该清理"视图模型取决于您的应用程序和视图模型的用法.例如,我在使用选项卡式界面的应用程序上工作.当用户关闭选项卡时,应用程序会调用表示该选项卡的viewmodel的cleanup(它本身会通过其viewmodel并对其进行调用cleanup).因此,一般规则-不再需要viewmodel时-应该清理它(关闭子窗口,标签等).至于其他问题:

When viewmodels should be "cleaned up" depends on your application and viewmodels usage. For example, I work on application with tabbed interface. When user closes tab application calls cleanup on viewmodel representing that tab (which itself goes through its viewmodels and calls cleanup on them as well). So the general rule - as soon as you don't need viewmodel anymore - you should cleanup it (closing child window, tab, etc.) As for the other questions:

2)如果您清理视图模型,那么关闭应用程序确实无关紧要.与关闭时一样,所有内存都将被释放,并且不会出现内存泄漏:)

2) It is indeed doesn't matter on application closing if you cleanup your viewmodels. As on closing all memory will be freed up and you don't get memory leaks :)

3)您应检查应用程序内存使用情况.在我们的应用程序中,我们遇到了内存泄漏的严重问题(实际上仍然有但不是很大).我们确定通过内存跟踪可以导致泄漏:打开/关闭了许多名为GC.Collect()的选项卡-但是内存使用率并未下降.我们开始使用WinDbg跟踪内存泄漏,并确实很多找到了我们未从Messenger中取消注册收件人的地方.另外,我们使用的是旧版本的MVVM Light,该版本已绑定到CommandManager,所以我们在RelayCommands上也遇到了问题.

3) You should check application memory usage. In our application we had serious problems (and actually still have but not that big) with memory leaks. We determined that we could have leaks by memory tracking: opened/closed many tabs, called GC.Collect() - but memory usage didn't go down. We started to track memory leaks with WinDbg and found literally lots of places where we didn't unregistered recipients from Messenger. Also, we're using and old version of MVVM Light which is bound to CommandManager so we had problems with RelayCommands as well.

道德是-您应该考虑在编程过程中清理资源,因为稍后查找和修复资源可能会很痛苦.

Moral is - you should think about cleaning up resources during programming as later it can be painfull to find and fix it.

这篇关于mvvm light icleanup和Messenger在VB.NET中注销的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆