不,您可以在一个视图模型中完成所有这些操作。视图模型的工作是保存视图的数据,并在必要时转换该数据以便视图可以使用它。没有什么规定视图模型必须保存特定类型的信息,即没有规定“它只能保存客户信息而不是订单信息”的规则。
话虽如此,视图模型也没有理由不能被几个不同的视图使用(当然,给它们所有不同的实例) - 这表明您在视图和视图模型之间有很好的关注点分离。就个人而言,我对我的视图模型进行编码,以便他们不知道视图存在。尽管将 CustomerView 绑定到 CustomerOrderProductViewModel 有点太过分了,但视图也没有理由必须消耗视图模型公开的所有内容。
编辑:让我再解释一下最后一段。
Example 1: i have a new V which shows customer information, and i have an existing VM which has customer info AND order info
我不愿意将此 VM 用于此 V,因为虽然它确实具有我需要的客户端信息,但它包含的信息太多 - 我在上下文之外使用 VM。
Example 2: i have a VM that contains full client info, like their postal and residential address, their phone numbers, their girlfriend's names*, and a bunch of other client type info. I have a V which shows the client's name, birthday, and maybe two other properties.
我会考虑将该 VM 用于该 V,这说明了我的观点,即 V 不必显示 VM 中包含的所有信息。当您更改 V 时,这个概念变得更加明显(例如,进行一些维护并更改视图的 UI,因为有人已经决定他们想要删除一堆字段,现在他们想要将客户的年龄表示为图像) - 我仍然可以使用相同的 VM,只需更改 V(我可以使用 ValueConverter 将年龄表示为图像,从而避免更改 VM)。
At what point would i stop using the current ClientViewModel, and write a new one that was more specific to the needs of the modified ClientView?
对此没有明确的答案 - 这取决于我可用于进行更改的时间量、可用于测试的时间量以及使用完整的客户端对象之间的复杂性权衡VM,并编写一个新的精简版本的 Client 对象,同时仍保留旧版本。
*这应该是包含在客户端对象中的独立对象的集合,但在这里与我一起工作,这是一个虚构的例子:)