使用Silverlight MVVM与棱镜/统一,需要检测时关闭视图 [英] Using Silverlight MVVM with Prism/Unity, and need to detect when view is closed

查看:198
本文介绍了使用Silverlight MVVM与棱镜/统一,需要检测时关闭视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写使用MVVM(模型 - 视图 - 视图模型)模式的应用程序,并正在利用从Microsoft P和棱镜和Unity位; P团队

I am writing an app using the MVVM (Model-View-ViewModel) pattern and am leveraging the Prism and Unity bits from the Microsoft P&P team.

我有一个项目列表视图。这些项目都包含在视图模型一个ObservableCollection到在视图列表框是数据绑定(视图模型设置为视图的DataContext的)。在视图模型,我有一个计时器运行触发该服务器每30秒的新数据的调查。当数据返回我名帅它交给UI线程和新数据添加到的ObservableCollection。这一切工作真的很好。

I have a View with a list of items. These items are contained with an ObservableCollection in the ViewModel to which a listbox in the View is databound (the ViewModel is set as the DataContext of the View). In the ViewModel, I have a timer running that fires a poll of the server for new data every 30 seconds. When the data returns I marshal it over to the UI thread and add the new data to the ObservableCollection. This all works really well.

我的问题是,我需要计时器视图时关闭,停止。我不知道看什么事件在这里。有什么在Unity会告诉我什么时候该观点已经在该地区被取代?有没有这将是最好用这个从视图中,或许传递的事件(或一个门面)的模型视图的事件吗?我的看法是每个P和一个用户控件,P的例子。有没有卸载事件中,我能找到也没有覆盖的方法。

The problem I have is that I need the timer to stop when the view is closed. I am not sure what event to watch for here. Is there something in Unity that will tell me when the view has been replaced in the region? Is there an event which would be best to use for this from the view, and perhaps pass that event (or a facade) on to the ModelView? My View is a UserControl per P&P examples. There is no "Unload" event I could find nor a method to override.

我已经在考虑写我自己的服务来处理视图改变(一些门面为在RegionManager),并可能只是实施我的意见的通用接口做清理工作,或对他们实现IDisposable时,他们是从视图中删除。但是,如果没有与核心的Silverlight框架或Unity /棱镜的范围这样做的干净的方式,我宁愿走这条道路。

I am already thinking of writing my own service to handle view changes (some sort of facade for the RegionManager), and might just implement a common interface on my Views to do cleanup or implement IDisposable on them when they are removed from a view. However, if there is a clean way of doing this with the confines of the core Silverlight framework or Unity/Prism, I would prefer to take that path.

编辑 - 答:

我结束了简单的标记安德森艾姆斯的答案被挑选一个是最接近我在做什么我的解决方案。不过说真的,我现在用的零件从PL和GraemeF以及和上投的每一个人。这对我来说是很大的反响,因为它给了我一些更深入地了解区域,给了我另一个框架来看待,并验证我可能会下来实现服务来处理视图的变化,而不是只调用了正确的道路。到RegionManager

I ended up marking Anderson Imes's answer simply by picking the one that was closest to what I am doing for my solution. But really, I am using parts from from PL and GraemeF as well and up-voted everyone. This was a great response for me, as it gave me some better insight into the regions, gave me another framework to look at, and verified that I was probably going down the right path with implementing a service to handle view changes instead of just calling into RegionManager.

推荐答案

我有这个确切的问题,最终基本上创造了这样的一个接口:

I had this exact problem and ended up basically creating an interface for this:

public IApplicationEvents
{
     void OnClose();
}



我集中我亲密的视图代码,基本上只是看着这个界面移除前视图。如果它在那里,我可以把它(我查了一下这两个视图本身也是DataContext属性如果它是一个的UIElement)。

I centralized my close view code and basically just looked for this interface before removing the view. If it was there, I could call it (I checked both the View itself and also the DataContext property if it was a UIElement).

我gussied它有点用附加属性和EventAggregator,但是这是基本概念和它工作得很好。

I gussied it up a little using an attached property and the EventAggregator, but this is the basic idea and it works well.

您是对的需要,当你查看关闭关闭下来,特别是如果你使用DispatcherTimer(你应该)。这件事导致如果不这样做了一些非常糟糕的内存泄漏。

You are right to need to shut this down when you view closes, especially if you are using DispatcherTimer (as you should). This thing causes some really bad memory leaks if you don't.

这篇关于使用Silverlight MVVM与棱镜/统一,需要检测时关闭视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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