使用Silverlight MVVM和Prism/Unity,需要检测视图何时关闭

发布于 2024-08-12 02:35:54 字数 863 浏览 7 评论 0原文

我正在使用 MVVM(模型-视图-视图模型)模式编写一个应用程序,并利用 Microsoft P&P 团队的 Prism 和 Unity 位。

我有一个带有项目列表的视图。这些项包含在 ViewModel 中的 ObservableCollection 中,视图中的列表框与该 ViewModel 进行数据绑定(ViewModel 被设置为视图的 DataContext)。在 ViewModel 中,我运行了一个计时器,每 30 秒触发一次服务器轮询以获取新数据。当数据返回时,我将其编组到 UI 线程并将新数据添加到 ObservableCollection。这一切都非常有效。

我遇到的问题是我需要计时器在视图关闭时停止。我不确定在这里要观看什么活动。 Unity 中是否有什么东西可以告诉我该区域的视图何时被替换?是否有一个事件最适合从视图中使用,并且可能将该事件(或外观)传递到 ModelView?我的视图是每个 P&P 示例的用户控件。我找不到“卸载”事件,也没有可以覆盖的方法。

我已经在考虑编写自己的服务来处理视图更改(RegionManager 的某种外观),并且可能只是在我的视图上实现一个通用接口,以便在从视图中删除它们时进行清理或实现 IDisposable。然而,如果有一种干净的方法可以在核心 Silverlight 框架或 Unity/Prism 的范围内做到这一点,我更愿意走这条路。

编辑 - 答案:

我最终通过选择与我正在为解决方案所做的最接近的答案来标记安德森·艾姆斯的答案。但实际上,我也使用了来自 PL 和 GraemeF 的部件,并对每个人都投了赞成票。这对我来说是一个很好的回应,因为它让我更好地了解了这些区域,给了我另一个可以查看的框架,并验证了我可能走的是正确的道路,实现了一个服务来处理视图更改,而不是仅仅调用进入区域管理器。

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.

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.

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.

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.

Edit - Answer:

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧梦荧光笔 2024-08-19 02:35:54

我认为您正在寻找的是区域管理器框架中的 IActiveAware 接口(请参阅 RegionActiveAwareBehavior< /a>):

RegionActiveAwareBehavior 负责通知视图是否处于活动状态。视图必须实现IActiveAware才能接收这些更改通知。这种主动感知通知是到达视图的单向方式;视图无法通过更改 IActiveAware 接口上的 active 属性来影响其活动状态。

I think what you are looking for is IActiveAware interface from Region Manager framework (see RegionActiveAwareBehavior):

The RegionActiveAwareBehavior is responsible for notifying a view if it is active or inactive. The view must implement IActiveAware to receive these change notifications. This active aware notification is one-way to the view; the view cannot affect its active state by changing the active property on the IActiveAware interface.

天冷不及心凉 2024-08-19 02:35:54

Caliburn 以其 IPresenter 组件模型 并且与 Prism 配合良好。您可以在 ViewModel 上实现 IPresenter 接口(或更可能使用其中一个基类),并在 OnShutdown 和/或 OnDeactivate 中停止计时器代码>.

您可以自己使用 Caliburn,或者看看它是如何实现的。

Caliburn addresses this missing piece of Prism with its IPresenter Component Model and works well with Prism. You would implement the IPresenter interface (or more likely use one of the base classes) on your ViewModels, and stop the timer in OnShutdown and/or OnDeactivate.

You could either use Caliburn yourself, or have a look and see how it is implemented.

放肆 2024-08-19 02:35:54

我遇到了这个确切的问题,最终基本上为此创建了一个界面:

public IApplicationEvents
{
     void OnClose();
}

我集中了我的关闭视图代码,基本上只是在删除视图之前寻找这个界面。如果它在那里,我可以调用它(我检查了 View 本身以及 DataContext 属性(如果它是 UIElement)。

我使用附加属性和 EventAggregator 对其进行了一些装饰,但这是基本思想,而且效果很好。

当您关闭视图时,您需要关闭它是正确的,特别是如果您正在使用 DispatcherTimer (正如您应该的那样)。如果你不这样做的话,这件事会导致一些非常严重的内存泄漏。

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

public IApplicationEvents
{
     void OnClose();
}

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).

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文