ViewModel事件注册和ViewModel生存期 [英] ViewModel Event Registration and ViewModel Lifetime

查看:73
本文介绍了ViewModel事件注册和ViewModel生存期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个体系结构问题,我想提出一个可能的解决方案.

I have an architectural problem, and a possible solution for which I would like an opinion.

我已经习惯了WP7的MVVM架构(只要有可能,不幸的是,有时sdk似乎朝相反的方向前进.)

I'm used to MVVM architecture for WP7 (whenever possible but unfortunately sometimes the sdk seems to go on the opposite direction).

WP7强制使用ViewFirst方法,对此我感到很舒服(除了我们无法覆盖View创建的部分(如Silverlight中那样)使构造函数注入成为可能).通过使大多数视图模型遵循其视图的实时性,我发现自己很有信心.因此,视图模型是在创建视图时创建的(通过访问ViewModelLocator),仅(或应该)通过视图引用ViewModel,当视图被破坏时,其ViewModel也应被破坏(其不是强制性的,而是其方式)除了极少数情况下,我会创建一个Singleton ViewModel.

WP7 force an ViewFirst approach and I feel confortable with this (except for the part that we can't override the View creation, as in Silverlight, to made constructor injection possible). I found myself confident by having most of the viewmodel follow the lyfetime of its view. So the viewmodel is created when the view is created (by accessing a ViewModelLocator), the ViewModel is (or should) be referenced only by its view, when the view is destroyed also its ViewModel should be destroyed (its not mandatory but its the way i go except in very rare case for which i create a singleton viewmodel).

我的视图模型可能需要注册一些单例服务事件(我创建的phoneservice或单例类).我的意思是,它可能需要注册一个类的事件,该类的生存期不是由视图模型本身决定的.这些事件保留对我的视图模型的硬引用,即使视图被破坏,也可以使我的视图模型保持活动状态,不仅如此,我的视图模型还将继续接收和处理事件.WeakEvent模式可能是一种可能的解决方案,但为每个事件创建一个事件管理器是不切实际的.我认为,最好的解决方案并不存在,而应该是对事件进行弱注册的关键词.

My viewmodel could need to register to some singleton service events (phoneservice or singleton class i created). I mean, it could need to register to an event of a class which lifetime is not decided by the viewmodel itself. These events keep an hardreferences to my viewmodel and keep my viewmodel alive even when the view is destoryed, not only, my viewmodel will continue to receive and process the events. The WeakEvent pattern could be a possible solution but it's unpractible to create an eventmanager for every event. The best solution, in my opinion, does not exist and should be a keyword for a weak registration to events.

我发现的一个解决方案是让我的视图模型知道NavigateTo和NavigateFrom,以便我可以从那里注册和注销事件.我还可以添加一些逻辑(例如,只有在返回的情况下才可以取消注册),以使NavigateTo和NavigateFrom中具有镜面逻辑.

A solution I found is to have my viewmodel aware of NavigateTo and NavigateFrom so i can register and unregister events from there. I can also add some logic (for example i could unregister only in case of back) putting attention to have specular logic in NavigateTo and NavigateFrom.

另一种可能的方法(我尚未测试过)可能是使我的视图模型知道View终结,并在视图终结时执行一些清理操作,但我始终觉得由于采用终结法,因此不建议使用此方法.我也不清楚性能会受到多大的影响.

Another possible way (I have not tested) could be to make my viewmodel aware of View finalization and perform some cleanup when the view is finalized but I always had the feeling the this approach is not reccomended beacuse of the use of the finalization. Also it's not clear to me how much the performance will be affected.

您如何看待ViewModel的生存期与其视图相同(到目前为止一直简化我的应用程序)?您如何看待NavigateTo-NavigateFrom ViewModel解决方案?您如何看待View-Finalization ViewModel解决方案?您是否经历过上述任何一种解决方案,或者是否经历过另一种解决方案?

What do you think about having the viewmodel lifetime be the same as its view (it always simplified my app until now) ? What do you think about the NavigateTo-NavigateFrom ViewModel aware solution ? What do you think about the View-Finalization ViewModel aware solution ? Have you experienced any of these or maybe another type of solution ?

问候天空G

更新

我发现终结解决方案将无法完成工作,因为它可能会在以后发生(或者可能永远不会发生).就目前而言,在我看来,最好的解决方案是在视图模型库中为视图应调用的事件注册取消注册提供一对虚拟方法.在加载和卸载事件期间可能会调用它们(当我不在后续视图中时,当我不需要我的viewmodel处理该事件时,在这种情况下,第一个视图/viewmodel仍在后台运行,但是如果视图的视图已附加/分离到可视化树,则会触发已加载/已卸载的对象.

I found that the finalization solution will not do the work beacuse it can happen in late future (or maybe never). For now it seems to me that the best solution is a pair of virtual method in the viewmodelbase Initialize and Cleanup for event registration deregistration which the view should call. A Possible moment to call them could be during loaded and unloaded event (when I don't need my viewmodel process the event if I'm in a subsequent view, in this this case the first view/viewmodel are still alive in the backstack but loaded/unloaded are fired if they view is attached/detached to the visual tree).

任何其他观点(甚至是赞同的观点)都将受到赞赏.

Any other opinion (even confermative) will be appreciated.

谢谢

推荐答案

我同意弱事件的想法很好,但是实现起来太麻烦了.它还会产生自己的问题-它可以使viewmodel完全弱引用,使其在实际应该"(开发人员意见vs垃圾收集者意见)之前成为垃圾收集方式的候选者.我之前曾被沃德·贝尔(Ward Bell)咬伤,在此博客中他的职位.

I agree that the weak event idea is nice but it would be too cumbersome to implement. It also creates issues of its own - it can make the viewmodel entirely weak-referenced, making it a candidate for garbage collection way before it actually "should" (developer opinion vs garbage collector opinion). I've been bitten by this before as has Ward Bell in this blog post of his.

根据您的要求和希望采用纯"方法制作MVVM(视图模型不了解视图),您似乎正在努力平衡MVVM的视图"和模型"之间的平衡视图模型.在这种情况下,我实际上会建议另一种设计模式:MVPVM(模型/视图/演示者/视图模型).这是 MSDN文章.

Based on your requirements and desire to follow a "pure" approach to MVVM (viewmodel doesn't know about the view), it seems like you are struggling with the balance between the "view" and the "model" of the viewmodel. In this case, I'd actually suggest another design pattern: MVPVM (model / view / presenter / viewmodel). Here's an MSDN article on it.

在您的情况下,演示者将保留这些单例事件.演示者知道视图是可以的,因为演示者并不打算在视图之间重用.这样,您的viewmodel就会变成视图模型",从而可以重用并消除大多数vm生命周期问题.

In your case, the presenter would hold on to those singleton events. It would be okay for the presenter to know about the view because the presenter isn't meant to be reused across views. Your viewmodel would then just become a "model of the view", allowing for reuse and eliminating most vm lifetime issues.

我喜欢MVPVM方法,因为当我的视图模型开始承担太多责任(与数据访问层交谈,侦听应用程序范围的事件以及从视图处理命令,维护其视图)时,它始终使MVVM与我息息相关视图的属性等).我知道这不一定能回答您的问题,但是评论太久了.

I like the MVPVM approach because it always concerned me in MVVM when my viewmodels started to take on too much responsibility (talking to the data access layer, listening to application-wide events as well as handling commands from the view, maintaining its properties for the view, etc). I know this isn't necessarily an answer to your question but it's too long for a comment.

这篇关于ViewModel事件注册和ViewModel生存期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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