每个UI窗口都有自己的ViewModel

每个UI窗口都有自己的ViewModel

本文介绍了使用MVVM,每个UI窗口都有自己的ViewModel?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

当我在MVVM模式下设计多个视图时,每个视图是否都有自己的ViewModel,或者它们是否共享相同的视图?我知道这最终是一个灵活的决定,但最好的做法是什么?

When I'm designing multiple views under the MVVM pattern, does each view get its own ViewModel or do they all share the same one? I understand that this is ultimately a flexible decision, but what is the best practice?

我的直觉告诉我每个视图都有一个ViewModel(即每个单独的UI窗口) 。 MVVM的所有博客示例显示单个视图,但不超过此视图。

My gut tells me to have a ViewModel for each view (i.e. each separate UI window). All of the blog examples of MVVM show a single view but not much beyond that.

推荐答案

是的,基本上这个想法是,你的viewModel只能由一个视图使用。如果您使用viewModel来填充区域(如在ASP.NET MVC中),那么每次该视图在不同的地方显示时,该视图模型就被重复使用。

Yes, basically the idea is that your viewModel should only be used by one view. If you use a viewModel to populate an area (like in ASP.NET MVC) then that viewModel is "reused" each time that view is presented in difference places.

是Josh Smith对MVVM模式的讨论。然后,Ward Bell在这个

This article is a discussion by Josh Smith of the MVVM pattern. Then, Ward Bell discusses in this article what he thinks Josh left out, while maintaining that Josh's work is still very strong.

Ward做了一个很好的工作来解决这个问题的复杂性模式并显示存在的紧张局势。这是他的紧张局势:

Ward does an excellent job of laying out the complexities of this pattern and showing the tension that exists. Here is his take on the tension:

另一方面,在业务应用程序中,应用程序的必要条件 - 视图必须做什么满足业务需求 - 是程序员的省份,并且通过ViewModel的功能进行了最佳表达。

On the other hand, in business applications the application’s imperatives – what the view must do to satisfy business requirements – are the province of the programmer and are > best articulated through the capabilities of the ViewModel.

其中包括View和ViewModel设计之间的必要紧张。作为开发人员,我的忠诚是使用ViewModel(应用程序应该做一些有价值的事情),但是以牺牲视图为代价来保护效忠是愚蠢的(一个好的UX对于使应用程序变得简单易学,使用)。

Therein lies the necessary tension between View and ViewModel design. As a developer my allegiance is with the ViewModel ("the application should do something worthwhile") but it would be silly to defend that allegiance at the expense of the View ("a good UX is essential to making an application easy to learn and to use").

这篇关于使用MVVM,每个UI窗口都有自己的ViewModel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 19:44