简体   繁体   English

不是 Singleton ViewModel 并且仍在同步

[英]Not Singleton ViewModel and still synchronizing

I would like to have two (or even more) different WPF windows (using MVVM!) but bound to the same type ViewModel .我想要两个(甚至更多)不同的 WPF windows (使用 MVVM!)但绑定到相同类型的ViewModel But the main thing is that I need those two windows to have synchronized content, both are displaying the same observable collection.主要的是我需要这两个 windows 具有同步内容,两者都显示相同的可观察集合。

Synchronizing would be easy with Singleton Pattern design of that ViewModel but I was told that singleton ViewModels is a terrible design.使用该ViewModel的 Singleton 模式设计进行同步很容易,但有人告诉我 singleton ViewModels 是一个糟糕的设计。

So, What is the best strategy where and when to assign datacontext and where to create ViewModels to solve this scenario?那么,在何时何地分配数据上下文以及在何处创建ViewModels以解决这种情况的最佳策略是什么?

ViewModels can have properties exposing other ViewModels. ViewModel 可以具有暴露其他 ViewModel 的属性。 The View for the outer ViewModel can have a ContentControl bound to the inner ViewModel.外部 ViewModel 的 View 可以有一个ContentControl绑定到内部 ViewModel。

In this way, you can have separate ViewModel instances that are bound the the Windows themselves, and only the part that needs to be the same between those 2 Views should be in the shared inner ViewModel.通过这种方式,您可以拥有单独的 ViewModel 实例,这些实例绑定了 Windows 本身,并且只有这两个 View 之间需要相同的部分应该在共享的内部 ViewModel 中。 So your inner ViewModel could expose data like a collection, while the separate ViewModels bound to their respective Windows can keep track of the selected item in the collection.因此,您的内部 ViewModel 可以像集合一样公开数据,而绑定到各自 Windows 的单独 ViewModel 可以跟踪集合中的选定项目。

But I think normally people make sure the data itself (Models) are shared, if at all, rather than ViewModels, as ViewModels tend to encapsulate behavior in addition to data, and you have to be very careful about an action on one Window doing the exact same thing as the same action on a different Window.但我认为通常人们确保数据本身(模型)是共享的,如果有的话,而不是 ViewModel,因为 ViewModel 倾向于封装除数据之外的行为,并且你必须非常小心对 Window 执行的操作与不同 Window 上的相同操作完全相同。 It carries a high risk of confusing the user due to unexpected side-effects.由于意外的副作用,它具有使用户感到困惑的高风险。

As for how to make this easier to do, if you're not using an MVVM framework, you might try one.至于如何让这更容易做到,如果你没有使用 MVVM 框架,你可以尝试一个。 MVVM Light as a good one to start with if you haven't used one, though I personally use Caliburn Micro .如果您还没有使用过MVVM Light ,那么它是一个不错的选择,尽管我个人使用的是Caliburn Micro MVVM Light has a template for a ViewModelLocator you might use to control ViewModel creation/reference, and you basically edit/add to the ViewModelLocator yourself. MVVM Light 有一个 ViewModelLocator 模板,您可以使用它来控制 ViewModel 创建/引用,并且您基本上可以自己编辑/添加到 ViewModelLocator。 Both play well with IOC containers, which would give you finer control over ViewModel creation.两者都可以很好地与 IOC 容器配合使用,这将使您可以更好地控制 ViewModel 的创建。

For example, any time a new Window for the outer ViewModel is opened, you've registered that ViewModel with the IOC container such that a new instance is created each time.例如,每当打开外部 ViewModel 的新 Window 时,您已经向 IOC 容器注册了该 ViewModel,因此每次都会创建一个新实例。 And when the outer ViewModel needs a instance of the inner ViewModel, you've registered a single instance in the container that gets returned for that, rather than a new instance being created each time.当外部 ViewModel 需要内部 ViewModel 的实例时,您已经在容器中注册了一个实例,该实例会为此返回,而不是每次都创建一个新实例。 (Whether this happens by using the IOC container as a Service Locator or as actual Dependency Injection is up to you.) (这是通过使用 IOC 容器作为服务定位器还是作为实际的依赖注入来实现,这取决于您。)

This article may be of interest, as well. 这篇文章可能也很有趣。

Please comment if you need anything clarified in regards to your original question, as it was a little broad.如果您需要对原始问题进行任何澄清,请发表评论,因为它有点宽泛。 There are simply a lot of ways to do what you want to do.有很多方法可以做你想做的事。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM