MVVM模式和Silverlight [英] MVVM pattern and Silverlight

查看:129
本文介绍了MVVM模式和Silverlight的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在使用MVVM模式实现我的第一个Silverlight 3应用程序,对此我有一些疑问.

是否可以在视图模型中嵌套视图模型?在这种情况下,我有一个主页,其中存储了多个用户控件,每个用户控件都在不同的应用程序区域中;或者在这种情况下,我想存储可以被其他视图模型访问的基于视图的模型属性. br/>
可以将视图模型创建为独立的视图模型,而不将其链接到模型吗?在这种情况下,我需要使用已经在应用程序中加载的数据来动态创建菜单,或者输入来自应用程序调用者的数据.

谢谢.

问候.

解决方案

是否可以在视图模型中嵌套视图模型?

是:只需创建视图模型类型的属性.一个真正的好主意是拥有一个IViewModel (继承自INotifyPropertyChanged)和一个抽象的ViewModel基类.您甚至可以在主视图模型类中将子视图模型属性设置为IViewModel类型,这样它将采用您喜欢的任何类型.

可以将视图模型创建为独立的视图模型,使其不链接到模型吗?



是的,这只是打破了格局.最好有一个模型来在视图模型之外创建菜单信息,因为这样可以将显示菜单的任务与创建菜单的任务分离开来,但是在最后一点是YMMV. /blockquote>

您可能还想考虑使用 Microsoft Prism [ 解决方案

Is it possible to nest a view model within a view model?

Yes: Just create a property of the view model type. A really good idea is to have an IViewModel (which inherits from INotifyPropertyChanged ) and an abstract ViewModel base class. You can even make the child view model property in the main view model class of type IViewModel so it''ll take any type you like.

Can a view model created as a stand alone view model, so not linked to a model?



Yes, it just breaks the pattern. It is probably better to have a model to create the menu information outside of the view model, as this de-couples the task of displaying the menu from creating the menu, but YMMV on this last point.


You might also want to consider using Microsoft Prism[^].

Any view consisting of another view could actually be broken up into sub regions within regions thus breaking your complex view into smaller ones. You will need to have a look at the Prism documentation and understand it before you start using though.


Yes, it just breaks the pattern. It is probably better to have a model to create the menu information outside of the view model, as this de-couples the task of displaying the menu from creating the menu, but YMMV on this last point.


The issue here is that most of the menu is built statically an dynamic part is built with data which comes as input in the Silverlight application. So I thought that menu view model may not be linked to a model because related to the UI only. That view model may be used associated with a model in the future for those menus that are built entirely from a source, such as XML or DB.

Also, is view model responsibility to work on the model and or Business Object in a Line of Business application? That''s why I adopted to create this command from a view model not linked to a model in this case.
Otherwise, I would create the model, than the view model to be bound to the view in the main view model, which I think it''s more tricky and resource expensive in this case.

Thanks.


这篇关于MVVM模式和Silverlight的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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