在MVVM中,每个视图模型只能有一个视图?
正如我在这里读到的:
http://msdn.microsoft .com/en-us/library/gg405484(v=pandp.40).aspx
通常是一对一的 视图及其之间的关系 查看模型。
这意味着从设计上来说,它们并不能真正处理多个视图?
As I read here:
http://msdn.microsoft.com/en-us/library/gg405484(v=pandp.40).aspx
There is typically a one-to-one
relationship between a view and its
view model.
It means that by design they don't really cope with multiple views ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我想实践中通常都是这样的。然而,将演示文稿分离为视图和视图模型的美妙之处在于,您可以轻松创建许多不同的视图,每个视图都显示模型中基本相同的数据,所有视图共享相同的 ViewModel 类(可能相同,也可能不同实例)。例如,我可以有一个简单和高级数据视图,主要用XAML编写为两个完全不同的UserControl,两者共享相同的ViewModel(类或实例) 。如果不使用 MVVM,那么如果不重复代码,这将变得更加棘手。
I think this is usually the case in practice. However, the beauty of separating the presentation into a View and a ViewModel means that you could easily create many different Views, each showing basically the same data from the Model, all sharing the same ViewModel class (maybe or maybe not the same instance). For example, I could have a simple and advanced View of my data, written mostly in XAML as two completely different UserControl's, both sharing the same ViewModel (class or maybe instance). Without using MVVM this would be trickier to do without duplicating code.
我认为这没有限制,这完全取决于您的设计和要求。您可以为单个 ViewModel 创建多个 View 以呈现不同的 UI 表示。
I think there is no restriction of this, it totally depends on your design and requirement. You can create multiple View for a single ViewModel to present different UI representation.
在 MVVM 中,您有一个视图(演示文稿)和一个视图模型(逻辑),旨在支持演示文稿的需求。您可以轻松地为 ViewModel 拥有多个视图,并且通常这是预期的,主要是在您有多个 DataTemplate 对象(即视图)根据使用的上下文绑定到单个 ViewModel 类型的情况下。
虽然这些通常是一对一的映射,但这不是一个限制,而是一个约定,并且 MVVM 模式中不存在“设计限制”。
In MVVM, you have a View (presentation) and a ViewModel (logic) which is intended to support that presentations needs. You can easily have multiple views for a ViewModel, and often this is expected, mostly in the case where you have multiple DataTemplate objects (which is a view) bound to a single ViewModel type based on the context in which it is being used.
While there is typically a one to one mapping of these, this is not a limitation but a convention and there is no "design limitation" in the MVVM pattern.
正确的。通常,视图模型是专门为一个视图设计的。它对视图中使用的特定控件没有任何了解,但它确实具有结构和功能知识。拥有多个视图和一个视图模型通常会使视图模型类违反单一责任原则。
尽管如此,有时拥有多个视图和一个视图模型确实有意义。例如,同一视图的基本版本和高级版本。在基本版本中,您只需隐藏一些部分或以更简单的方式和简化的功能来呈现信息。在这种情况下,为这两个视图使用一个视图模型是绝对可以的,否则您将不得不复制视图模型的大部分代码。
MVVM 模式不强制视图和视图模型之间的一对一关系,但在大多数情况下它是推荐的方法。如果您想为一个视图模型拥有多个视图,那么在走这条路之前您应该仔细考虑,因为您最终可能会得到一个视图模型,其中一半的成员由一个视图使用,另一半由另一个视图使用。
Correct. Typically, view model is designed specifically for one view. It doesn't have any knowledge about specific controls used in the view, but it does have the structural and functional knowledge. Having multiple views and one view model would often make the view model class violate the single responsibility principle.
Although, sometimes it does make sense to have several views and one view model. For instance, basic and advanced version of the same view. In basic version you just hide some parts or present the information in a simpler way with simplified functionality. In this case it is absolutely OK to have one view model for those two views because otherwise you would have to duplicate most of the view model's code.
The MVVM pattern doesn't enforce the one-to-one relationship between view and view model, but in most cases it is the recommended approach. And if you want to have multiple views for one view model you should think carefully before going that path because you might end up with a view model which has half of its members used by one view and the other half by another.
我实际上利用了这样一个事实:这种一对一的关系在项目中并未强制执行。
我们有一个警报虚拟机,我们希望将它们显示在我们的视图之一的列表中,但当有新警报时也会弹出通知并在主屏幕中执行此操作。
通过将弹出控件的数据上下文设置为警报虚拟机,我们可以轻松实现此功能。
I actually used the fact that this one-to-one relationship is not enforced in a project.
We had an alerts vm, and we wanted to show them in a list on one of our views, but than also popup a notification when there's a new alarm and do it in the main screen.
By setting the data context of the popup control to the alarms vm, we could easily achieve this functionality.