什么是ViewModelLocator?与DataTemplates相比,它的优缺点是什么? [英] What is a ViewModelLocator and what are its pros/cons compared to DataTemplates?

查看:123
本文介绍了什么是ViewModelLocator?与DataTemplates相比,它的优缺点是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以给我一个关于ViewModelLocator是什么,它如何工作以及与DataTemplates相比使用它的利弊的简短摘要吗?

Can someone give me a quick summary of what a ViewModelLocator is, how it works, and what the pros/cons are for using it compared to DataTemplates?

我曾尝试在Google上查找信息,但似乎有许多不同的实现方式,但没有明确列出它的含义以及使用它的利弊.

I have tried finding info on Google but there seems to be many different implementations of it and no striaght list as to what it is and the pros/cons of using it.

推荐答案

简介

在MVVM中,通常的做法是通过从依赖项注入中解析视图来让视图找到其ViewModel.

(DI)容器.当要求容器提供(解析)View类的实例时,这会自动发生.容器通过调用接受ViewModel参数的View的构造函数,将ViewModel注入.这种方案称为控制反转(IoC).

Intro

In MVVM the usual practice is to have the Views find their ViewModels by resolving them from a dependency injection (DI) container. This happens automatically when the container is asked to provide (resolve) an instance of the View class. The container injects the ViewModel into the View by calling a constructor of the View which accepts a ViewModel parameter; this scheme is called inversion of control (IoC).

这里的主要好处是可以在运行时配置容器,其中包含有关如何解析我们向其请求的类型的说明.通过指示它解析我们在应用程序实际运行时使用的类型(Views和ViewModels),从而提供了更高的可测试性,但是在为应用程序运行单元测试时以不同的方式指示它.在后一种情况下,应用程序甚至没有UI(它没有在运行;只是测试而已),因此容器将解析

The main benefit here is that the container can be configured at run time with instructions on how to resolve the types that we request from it. This allows for greater testability by instructing it to resolve the types (Views and ViewModels) we use when our application actually runs, but instructing it differently when running the unit tests for the application. In the latter case the application will not even have a UI (it's not running; just the tests are) so the container will resolve mocks in place of the "normal" types used when the application runs.

到目前为止,我们已经看到,DI方法通过在创建应用程序组件的过程中添加抽象层,使应用程序易于测试.这种方法有一个问题:它不适用于视觉设计器,例如Microsoft Expression Blend.

So far we have seen that the DI approach allows easy testability for the application by adding an abstraction layer over the creation of application components. There is one problem with this approach: it doesn't play well with visual designers such as Microsoft Expression Blend.

问题在于,在正常的应用程序运行和单元测试运行中,都必须设置容器,以说明要解决的类型;另外,有人必须询问容器来解析视图,以便可以将ViewModels注入其中.

The problem is that in both normal application runs and unit test runs, someone has to set up the container with instructions on what types to resolve; additionally, someone has to ask the container to resolve the Views so that the ViewModels can be injected into them.

但是,在设计时没有我们正在运行的代码.设计者尝试使用反射来创建我们的View实例,这意味着:

However, in design time there is no code of ours running. The designer attempts to use reflection to create instances of our Views, which means that:

  • 如果View构造函数需要一个ViewModel实例,那么设计人员将根本无法实例化View,它将以某种受控方式出错
  • 如果View具有无参数的构造函数,则将实例化该View,但其DataContext将为null,因此我们将在设计器中获得一个空"视图-这不是很有用
  • If the View constructor requires a ViewModel instance the designer won't be able to instantiate the View at all -- it will error out in some controlled manner
  • If the View has a parameterless constructor the View will be instantiated, but its DataContext will be null so we 'll get an "empty" view in the designer -- which is not very useful

ViewModelLocator是这样使用的附加抽象:

The ViewModelLocator is an additional abstraction used like this:

  • 视图本身将ViewModelLocator实例化为其资源的一部分将其DataContext绑定到定位器的ViewModel属性
  • 定位器以某种方式检测我们是否处于设计模式
  • 如果不在设计模式下,则定位器将返回一个从DI容器解析的ViewModel,如上所述.
  • 如果处于设计模式,则定位器将使用其自己的逻辑返回固定的虚拟" ViewModel(请记住:设计时没有容器!);这个ViewModel通常会预先填充虚拟数据
  • The View itself instantiates a ViewModelLocator as part of its resources and databinds its DataContext to the ViewModel property of the locator
  • The locator somehow detects if we are in design mode
  • If not in design mode, the locator returns a ViewModel that it resolves from the DI container, as explained above
  • If in design mode, the locator returns a fixed "dummy" ViewModel using its own logic (remember: there is no container in design time!); this ViewModel typically comes prepopulated with dummy data

当然,这意味着View必须具有一个无参数的构造函数(否则设计器将无法实例化它).

Of course this means that the View must have a parameterless constructor to begin with (otherwise the designer won't be able to instantiate it).

ViewModelLocator是一个惯用法,可以让您在MVVM应用程序中保留DI的优点,同时还可以使您的代码与可视设计人员一起很好地发挥作用.有时这称为应用程序的可混合性"(指Expression Blend).

ViewModelLocator is an idiom that lets you keep the benefits of DI in your MVVM application while also allowing your code to play well with visual designers. This is sometimes called the "blendability" of your application (referring to Expression Blend).

在消化完上述内容后,请参见一个实际示例此处.

After digesting the above, see a practical example here.

最后,使用数据模板不是使用ViewModelLocator的替代方法,而是使用UI的显式View/ViewModel对的替代方法.通常,您可能会发现不需要为ViewModel定义View,因为您可以改用数据模板.

Finally, using data templates is not an alternative to using ViewModelLocator, but an alternative to using explicit View/ViewModel pairs for parts of your UI. Often you may find that there's no need to define a View for a ViewModel because you can use a data template instead.

这篇关于什么是ViewModelLocator?与DataTemplates相比,它的优缺点是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆