如何的ViewModels之间的WPF和交流如何控制视图的生命周期 [英] How to communicate between ViewModels in WPF and how to control Views lifecycle

查看:158
本文介绍了如何的ViewModels之间的WPF和交流如何控制视图的生命周期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有三个窗口主窗口,FirstWindow和SecondWindow。主窗口可以打开FirstWindow和SecondWindow。

There are three windows MainWindow, FirstWindow and SecondWindow. MainWindow can open FirstWindow and SecondWindow.

现在我的问题是:


  • 如何从FirstWindow打开SecondWindow和当SecondWindow打开关闭FirstWindow。在这个时候,我可以控制SecondWindow但并不能够控制主窗口,就像从主窗口使用SecondWindow.ShowDialog()。

  • 我点击SecondWindow保存按钮后,SecondWindow应关闭主窗口的DataGrid中应予以更新。如何更新从另一个视图模型数据或如何返回数据时,事件已处理?

推荐答案

我同意曾国藩的回答并会尽量延长他的回答。

I'm agree with Tseng's answer and will try to extend his answer.

第一部分

有关模块(不仅的ViewModels)之间的低耦合通信,我们可以尝试实施的 EventAggregator 格局。事件聚合有助于实现低耦合的应用程序用户/出版商模式。我知道很多不同的实现。

For low-coupled communication between modules (not only ViewModels) we can try to implement EventAggregator pattern. Event aggregator helps to implement subscriber/publisher pattern in low-coupled app. I know few different implementations.

根据的 CodeProject上后并使用WeakReference的,这将帮助您防止内存泄漏。我不会公布完整的代码,因为你可以下载源代码,并使用它。在此实现,则必须实现ISubscriber接口,你的用户。

First one based on CodeProject post and uses WeakReference that will help you to prevent memory leak. I will not publish whole code because you can just download source code and use it. In this implementation you must to implement ISubscriber interface for your subscribers.

二是的微软棱镜实现。这是一个开源项目,然后就可以看到接口 实施并的基本事件类。在此实现,则必须从事件手动退订。

The second is Microsoft Prism implementation. This is an open source project then you can see interface, implementation and base event class. In this implementation you must unsubscribe from the event manually.

第三和最后一个是MVVMLight库及其的信使类。

The third and the last is MVVMLight library and its Messenger class.

正如你可以看到这一切的实现都使用Singleton模式来保存用户。

As you can see all of this implementations uses Singleton pattern for saving subscribers.

第二部分

第二部分是关于导航。该simpliest的方法是使用页面导航基础设施。但是在MVVM世界,我们有多种导航不同的概念。

Second part is about navigation. The simpliest way is to use Page navigation infrastructure. But in MVVM-world we have many different concepts for navigation.

使用的导航抽象的主要目的是导航逻辑从具体的视图渲染技术分离(WPF,Silverlight的, WinRT的,Xamarin)。

The main purpose to use navigation abstractions is to separate navigation logic from concrete view rendering technology (WPF, Silverlight, WinRT, Xamarin).

例如,在的微软棱镜我们可以使用区域和的 RegionManager 的意见和窗口之间的导航。 。这是非常笨重的导航框架,它可以是很难理解只有一个文章后,这个概念

For example, in Microsoft Prism we can use regions and RegionManager for navigation between views and windows. It's very ponderous navigation framework and it can be difficult to understand the concept after only one article.

MVVM光也有自己的导航机制

MVVM Light also have their own navigation mechanism.

有关我的项目我用我自己的实现导航的通过的工作区。它是结合了从棱镜.NET和区域概念页面导航壮年兵的混合机制。

For my projects I use my own realization of navigation via Workspaces. It is a hybrid mechanism that combines Page navigation principes from .net and Regions concept from Prism.

结论

这文章是不是答案的答案。但我希望这将有助于你理解MVVM的概念。

This post is not an answer to your questions. But I hope that it will be helpful to you to understanding MVVM concepts.

正如你可以阅读上面有许多MVVM的框架,其中包含基础设施(不仅Messenger和的NavigationService的,但也基本命令实现,PopupService,转换器, INotifyPropertyChanged的 -helpers和的基础视图模型实现)来实现应用程序中的典型场景。

As you can read above there are many MVVM-frameworks which contains infrastructure (not only Messenger and NavigationService, but also base command realisations, PopupService, converters, INotifyPropertyChanged-helpers and base ViewModel implementations) to implement typical scenarios in your application.

这篇关于如何的ViewModels之间的WPF和交流如何控制视图的生命周期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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