我应该在Silverlight项目中使用Model-View-ViewModel(MVVM)模式吗? [英] Should I use the Model-View-ViewModel (MVVM) pattern in Silverlight projects?

查看:132
本文介绍了我应该在Silverlight项目中使用Model-View-ViewModel(MVVM)模式吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Silverlight控件的一个挑战是,当属性绑定到代码时,它们在Blend中不再是可以编辑的。例如,如果您有一个从数据Feed中填充的ListView,则在混合中编辑控件时,没有可见的元素。

One challenge with Silverlight controls is that when properties are bound to code, they're no longer really editable in Blend. For example, if you've got a ListView that's populated from a data feed, there are no elements visible when you edit the control in Blend.

我听说过由WPF开发社区发起的MVVM模式也可以帮助保持Silverlight控件可混合。我还在围着它,但这里有一些解释:

I've heard that the MVVM pattern, originated by the WPF development community, can also help with keeping Silverlight controls "blendable". I'm still wrapping my head around it, but here are some explanations:

  • http://www.nikhilk.net/Silverlight-ViewModel-Pattern.aspx
  • http://mark-dot-net.blogspot.com/2008/11/model-view-view-model-mvvm-in.html
  • http://www.ryankeeter.com/silverlight/silverlight-mvvm-pt-1-hello-world-style/
  • http://jonas.follesoe.no/YouCardRevisitedImplementingTheViewModelPattern.aspx

一个潜在的缺点是,该模式需要额外的类,尽管不一定更多的代码由...显示上面的第二个链接)。想法?

One potential downside is that the pattern requires additional classes, although not necessarily more code (as shown by the second link above). Thoughts?

推荐答案

我绝对认为您应该使用MVVM模式进行Silverlight应用程序,而且模式的一个好处是您实际上可以通过一些简单的技术使您的应用程序真正融合。我经常将可混合性称为可设计性设计 - 您使用某些技术确保您的应用程序在Blend中看起来非常棒。

I definitely think you should use the MVVM pattern for Silverlight applications - and one of the benefits of the pattern is that you can actually make your application really blendable through some simple techniques. I often refer to "blendability" as "design for designability" - that you use certain techniques to make sure your application looks great in Blend.

其中一种技术 - Torbjørn指出 - 是使用依赖注入框架,并提供外部服务的不同实现,这取决于在Blend或浏览器中执行的代码。所以当配置代码在Blend中执行时,我配置我的容器使用一个虚拟数据提供者,这样你就可以获得列表框,数据网格等的设计时间支持。

One of the techniques - like Torbjørn points out - is to use a dependency injection framework and supply different implementations of your external services depending on wether the code is being executed in Blend or in the Browser. So I configure my container to use a dummy data provider when the code is executing in Blend, and that way you get design time support for your list boxes, data grids etc.

通常情况下,挑战是如何以声明方式设置DataContext - 所以我经常会使用IoC容器的前端服务定位器类。这样我可以将数据上下文绑定到服务定位器上的一个属性。

The challenge is often how to set the DataContext declaratively - so I often end up using a service locator class a a "front end" to the IoC container. That way I can bind the data context to a property on the service locator.

另一种技术是创建一些具有两个属性的ObjectDataSource控件(非可视化):设计时间DataContext和RunTime数据上下文。控件执行检测正在执行的位置,然后将父DataContext设置为正确的对象。

Another technique is create some kind of ObjectDataSource control (non visual) that has two properties: Design Time DataContext and RunTime Data Context. The control does the job of detecting where is being executing, and then setting the Parent DataContext to the right object.

这篇关于我应该在Silverlight项目中使用Model-View-ViewModel(MVVM)模式吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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