WPF - MVVM -各种视图创建技术的优缺点是什么?

[英]WPF - MVVM - What are the pros and cons of the various view creation techniques?


I've been using MVVM for the past two years and it has certainly evolved for the better since that time. As I read though the hundreds of MVVM articles and stackoverflow questions, I've noticed that there appears to be more and more articles that describe the view/viewmodel relationship and creation as ViewModel first or View first. These articles usually use IoC or DI. I'd include some links but as a new SO user I'm only limited to 1.

在过去的两年里,我一直在使用MVVM,从那时起,MVVM的发展就越来越好了。当我阅读了数百篇MVVM文章和stackoverflow问题时,我注意到似乎有越来越多的文章将视图/视图模型关系和创建描述为viewmodel first或view first。这些文章通常使用IoC或DI。我将包含一些链接,但作为一个新用户,我只被限制为1。

My favorite technique has been the use of datatemplates to create the views, and structuring the application off the the viewmodels rather than the views. I rarely see articles anymore that use this pattern.

我最喜欢的技术是使用datatemplates创建视图,并在viewmodel上而不是视图上构造应用程序。我很少再看到使用这种模式的文章。

<DataTemplate DataType="{x:Type ViewModels:DummyViewModel}">
  <Views:DummyUserControl DataContext="{Binding}"/>
</DataTemplate>

Testability and decoupling seem to be the main focus of these 'non-datatemplate' V-VM creation/relationship designs and articles, and often they have to do with MEF or PRISM. Ultimately I would like to know the following:

可测试性和解耦似乎是这些“非数据占用板”V-VM创建/关系设计和文章的主要关注点,它们通常与MEF或PRISM有关。最后我想知道以下几点:

  1. Is the DataTemplate view creation technique still used or recommended?
  2. 是否仍然使用或推荐使用DataTemplate视图创建技术?
  3. What are the pros/cons of the view first design?
  4. 视图优先设计的优缺点是什么?
  5. What are the pros/cons of the viewmodel first design (with the view injected)
  6. viewmodel first设计的优缺点是什么(注入了视图)

I realize these are loose questions which deserve loose answers.

我意识到这些都是松散的问题,应该得到松散的答案。

Any good links to articles covering these topics is appreciated, provided they are not MEF/PRISM related. The more the better. (Google turns up quite a few)

如果它们不是MEF/PRISM相关的,那么任何关于这些主题的文章的链接都会被欣赏。越多越好。(谷歌出现了不少)

2 个解决方案

#1


2  

1.Is the DataTemplate view creation technique still used or recommended?

1。是否仍然使用或推荐使用DataTemplate视图创建技术?

This is my preferred method of operating in MVVM. I like this approach very much, for reasons I'll specify below. I use this in all of my development.

这是我在MVVM中首选的操作方法。我非常喜欢这种方法,原因如下。我在所有的开发中都用到这个。

2.What are the pros/cons of the view first design?

2。视图优先设计的优缺点是什么?

The main pro here I've found is that it's a bit easier in the design time experience. The designer "knows" the data context in advance, and tends to be able to do an easier job of working.

我在这里发现的主要优点是它在设计时的体验更容易一些。设计者事先“了解”了数据上下文,并且倾向于更容易地工作。

The main con here, from my perspective, is that you're adding a tighter coupling between the View and the ViewModel. It's also more difficult to choose a specific model for passing around.

在我看来,这里的主要问题是,您在视图和视图模型之间添加了更紧密的耦合。选择特定的模型进行传递也更加困难。

3.What are the pros/cons of the viewmodel first design (with the view injected)

3所示。viewmodel first设计的优缺点是什么(注入了视图)

I personally like this approach. This way, the "logical" side of your application is completely contained within your ViewModel layer. By using ContentPresenters, you can have the ViewModel easily generate other ViewModels, defining the "flow" of your application. The Views can be changed by a designer very easily.

我个人喜欢这种方法。这样,应用程序的“逻辑”端就完全包含在ViewModel层中。通过使用contentpresenter,您可以让ViewModel轻松生成其他的ViewModel,定义应用程序的“流”。视图可以被设计者很容易地改变。

The con here, though, is a slight decrease in design time usability, though- since the views don't really know anything about the VM at design time, you lose some designability.

不过,这里的缺点是设计时可用性略有下降——因为视图在设计时并不真正了解VM,因此您会失去一些可设计性。

#2


0  

We experienced that the DataTemplate approach has some limitations. By example the template is instantiated as shared (singleton) instance. In some scenarios this has undesirable side effects. That’s the reason we use the ViewModel first design (with the view injected).

我们经验到DataTemplate方法有一些局限性。通过示例,模板实例化为共享(单例)实例。在某些情况下,这有不良的副作用。这就是我们使用ViewModel first设计(注入视图)的原因。

The approach we use is described on the WPF Application Framework (WAF) project page:

我们使用的方法在WPF应用框架(WAF)项目页面上进行了描述:

http://waf.codeplex.com/wikipage?title=Model-View-ViewModel%20Pattern&ProjectName=waf

http://waf.codeplex.com/wikipage?title=Model-View-ViewModel%20Pattern&ProjectName=waf


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2010/01/26/742dbb92e4347d688dd08eacdaa7b26.html



 
粤ICP备14056181号  © 2014-2019 ITdaan.com

赞助商广告