PRISM 和事件聚合器
我有一个 PRISM WPF 应用程序;我有一个 PatientViewModel,它在其构造函数中订阅一个事件(比方说 CultureChangedEvent),并执行一个操作。
在我的主视图模型中,我有一个 ObservableCollection。
如果用户从 ObservableCollection 中删除 PatientViewModel 实例,然后触发 CultureChangedEvent,则删除的 PatientViewModel 仍位于内存中并接收该事件。因此,目前,当删除 PatientViewModel 时,我取消订阅该事件;但我想知道这是正确的方法吗?或者我错过了什么?
谢谢! L
I have a PRISM WPF application; and I have a PatientViewModel that in its constructor subscribes to an event (let's say CultureChangedEvent), and executes an action.
In my main view model I have an ObservableCollection.
If the user removes a PatientViewModel instance from the ObservableCollection, and then a CultureChangedEvent is fired, the removed PatientViewModel is still in memory and receives the event. So currently, when removing a PatientViewModel, I unsubscribe from the event; but I wonder of this is the correct approach? Or am I missing something?
Thanks!
L
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
即使对象从可观察集合中删除,它仍然存在(因此它仍然订阅事件)。您处理此问题的方式是可接受的解决方案。另一种是将 ObservableCollection 公开为 ReadOnlyObservableCollection,然后提供您自己的添加和删除函数。在您的删除函数中取消订阅事件,然后从您的私有“普通”ObservableCollection 中删除该项目。因此,您可以在删除项目之前取消订阅该活动。
Even though an object is removed from an observable collection it still exists (thus it still subcribes to the event). The way you are handling this is an acceptable solution. Another one is to expose your ObservableCollection as a ReadOnlyObservableCollection and then provide your own add and remove functions. Inside your Remove function unsubscribe from the event and then remove the item from your private "normal" ObservableCollection. Thus you can unsubscribe from the event before you remove the item.