Should I use the Model-View-ViewModel (MVVM) pattern in Silverlight projects?

前端 未结 12 2006
春和景丽
春和景丽 2020-12-22 17:25

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\'

12条回答
  •  独厮守ぢ
    2020-12-22 18:15

    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.

    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.

    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.

    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.

提交回复
热议问题