将视图模型连接到其视图的首选方式是什么?
过去,在使用 MVVM 时,我将每个视图创建为 DataTemplate
到其相应的视图模型来处理连接它们。我刚刚开始使用 MVVM Light,并注意到他们有 ViewModelLocator
。我看过的其他几个工具包包含了一些这种变体,但是这与使用 DataTemplate 相比有什么好处呢?
连接视图和视图模型的最佳实践是什么?
In the past while working with MVVM I've created every View as a DataTemplate
to it's corresponding viewmodel to handle connecting them. I just started using MVVM Light, and noticed they have the ViewModelLocator
. Several other toolkits I've looked at include some variation of this, but what benefit does this bring over using DataTemplate
?
Which is the best practice for connecting your Views and ViewModels?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有两种不同的方法,而不是一种“正确的方法”。
ViewModelLocator
或类似方法所帮助的方法是开发 MVVM 的“视图优先”方法。这意味着您从设计器中的视图开始,然后构建与之匹配的视图模型。从逻辑上讲,视图通常会创建其他视图,并且 ViewModel 通常通过某种形式的定位器加载并为给定视图填充。消息传递或服务用于将适当的模型挂接到新生成的 ViewModel 中。这样做的优点是更容易进行视觉设计,尤其是在使用 Blend 时。
另一种方法是“ViewModel-First”。这样,您就生成了 ViewModel,然后使用 DataTemplates 来填充视图。 ViewModels 将组合/创建其他 VM,直接设置适当的 Model。从程序员的角度来看,这通常(IMO)要干净得多,因为事情可以直接工作。然而,从设计者的角度来看,它通常更难以设计和使用,因为设计时数据更难以生成等。
这两种方法都完全有效,并且具有很大的优点和缺点。出于各种原因,不同的 MVVM 纯粹主义者倾向于选择一种方法而不是另一种方法 - 通常是从代码整洁的角度还是从设计人员的角度来实现这一点。
There are two different approaches, and not one "right way".
The approach that a
ViewModelLocator
or similar helps with is a "View-First" approach to developing MVVM. By this, it means you start with your View in the designer, and then build the ViewModel to match. Logically, Views often create other Views, and the ViewModel is typically loaded via some form of locator and populated for a given View. Messaging or services are used to hook appropriate models into the newly generated ViewModels.This has the advantage of being a bit easier to design visually, especially when working with Blend.
The other approach is to work "ViewModel-First". By this, you generate your ViewModels, and then use DataTemplates to have the View populate. ViewModels will compose/create other VMs, directly setting the appropriate Model. This is typically (IMO) much, much cleaner from a programmer's perspective, as things just work directly. However, it's typically more difficult to design and work with from a designer's point of view, as design-time data is more difficult to generate, etc.
Both approaches are perfectly valid, and have strong advantages and disadvantages. Different MVVM purists tend to prefer one approach over the other for various reasons - typically whether they're approaching this from a code cleanliness point of view or a designer point of view.
我的看法是:
DataContext
,这有时可能会出现问题。对于(3)(如何使用 ViewModelLocator):
在您的 xaml 中,您将像这样使用它:
对于此属性:
The way I see it:
DataContext
as soon as you create an instance of the UI.For (3) (How to use the ViewModelLocator) :
ViewModelLocator.cs
and typemvvmlocatorproperty
. Select it in the intellisense and double TAB for the snippet to work. Change it to the appropriate property.In your xaml you will use it like this:
For this property: