Windows Phone 8 MVVM

时间:2023-01-11 08:34:21

Why?

1 reason MVVM works really well with XAML based applications is because of the powerful XAML binding features. This allows the View (the presentation of to the user) to be separated from the data and the logic. The View can be designed in Expression Blend while the ViewModel can be developed in Visual Studio .NET. It allows for the presentation to be separated very easily. This is just 1 reason, albeit a powerful one.

View

A View is a class that represents the user interface that the user will see. In Silverlight this is the MainPage.xaml or Page.xaml class, for example. The View contains the visual controls that will be shown to the user and can also contain animations, navigation aspects, themes, and other interactive features for the purpose of the visual presentation. In Silverlight and WPF the View also contains binding extensions that identify the data points that will be presented to the user (embedded in XAML). The bindings point to the names of the data point properties, but do not have awareness of where those properties are nor where they come from. The bindings are activated when the View’s DataContext is set to a class that contains the source for the bindings.

Differing Opinions:

Some people create a View and assign it a source for its data right in the View. This can be done by creating a resource in the XAML that points to a ViewModel. I prefer to associate my View and ViewModel separate from each other so they are not coupled. however it is very common to see the ViewModel created inside the View as a resource. Either way is good. Creating it inside the View is easier and can allow for more fun when using Blend. Associating the ViewModel and the View through another class is looser and can allow for more DI and testability. Again, both are good and prominent in the wild.

Model

A class that represents data points describing a specific entity.  For example, a Customer class with properties such as CompanyName and CustomerId. The Model can contains child Model’s too, like any object graph. So a Customer Model can contain a property named Orders which is a set of Order Models. The Model’s purpose is to represent the data points and it has no knowledge of where it will be presented to a user nor how it will be presented. It’s single responsibility is to represent the data points.

Differring opinions:

Some people create the Model and give it awareness of where it comes from. I do not take this approach as I like to keep the Model ignorant of this and loosely coupled from where it comes form. However to offer up one different way to handle this it could help to discuss this at a high level. Some make the Model also have awareness of how to load and save itself (persistence awareness). For example, this could mean it knows how to call a web service to do this. Again, I do not do this. Rather I prefer to make that saving and loading of a Model happen in another class which has the single responsibility of handling these aspects.

ViewModel

The ViewModel is the glue between the View and the outside world. The ViewModel is what the View is bound to. The View’s DataContext is set to an instance of a ViewModel class. This is where all of the bindings declared in the View’s XAML The ViewModel contains the Model, so it has all of the data points needed in many cases right there. The ViewModel also can declare public properties for Commands (ways to invoke actions from the View to the ViewModel) and other properties that can be bound to the View.

Differing Opinions:

In my architecture, my ViewModel classes make calls to another class that handles filling the Model(s) contained within the ViewModel. Some other ways of doing this are to simply have the ViewModel go get the data himself. Again, I lean towards keeping my classes focused on a single responsibility so my examples generally do not do that. Instead, I have a class that is in charge of hitting a web service to go and get my data and fill my Model for my ViewModel.

Common Picture of the MVVM Triad

Windows Phone 8 MVVM

I hope this helps!

Windows Phone 8 MVVM的更多相关文章

  1. 背水一战 Windows 10 (25) - MVVM: 通过 x:Bind 实现 MVVM(不用 Command)

    [源码下载] 背水一战 Windows 10 (25) - MVVM: 通过 x:Bind 实现 MVVM(不用 Command) 作者:webabcd 介绍背水一战 Windows 10 之 MVV ...

  2. 背水一战 Windows 10 (24) - MVVM: 通过 Binding 或 x:Bind 结合 Command 实现,通过非 ButtonBase 触发命令

    [源码下载] 背水一战 Windows 10 (24) - MVVM: 通过 Binding 或 x:Bind 结合 Command 实现,通过非 ButtonBase 触发命令 作者:webabcd ...

  3. 背水一战 Windows 10 (23) - MVVM: 通过 Binding 或 x:Bind 结合 Command 实现,通过 ButtonBase 触发命令

    [源码下载] 背水一战 Windows 10 (23) - MVVM: 通过 Binding 或 x:Bind 结合 Command 实现,通过 ButtonBase 触发命令 作者:webabcd ...

  4. windows phone MVVM开发心得第一天

    之前刚刚学了asp.net网站的三层架构,为其中的优点着迷,可惜寒假本来决定学下MVC的计划泡汤了,刚开学,学了下windows phone 的MVVM模式的开发,在此留下点心得和脚印,第一天只是学了 ...

  5. MVVM: 通过 Binding 或 x:Bind 结合 Command 实现,通过 ButtonBase 触发命令

    介绍背水一战 Windows 10 之 MVVM(Model-View-ViewModel) 通过 Binding 或 x:Bind 结合 Command 实现,通过 ButtonBase 触发命令 ...

  6. MVVM: 通过 Binding 或 x:Bind 结合 Command 实现,通过非 ButtonBase 触发命令

    介绍背水一战 Windows 10 之 MVVM(Model-View-ViewModel) 通过 Binding 或 x:Bind 结合 Command 实现,通过非 ButtonBase 触发命令 ...

  7. MVVM: 通过 x:Bind 实现 MVVM(不用 Command)

    背水一战 Windows 10 之 MVVM(Model-View-ViewModel) 通过 x:Bind 实现 MVVM(不用 Command) 示例1.ModelMVVM/Model/Produ ...

  8. 什么是MVVM模式

    问题引入1 场景一:团队辛辛苦苦完成了一个项目,抱着激动的心情去给用户做demo,而用户给你的反馈是UI很不满意,要重新修改,否则拒绝验收.大规模修改UI,晴天霹雳!2 场景二:产品在一家客户上线运行 ...

  9. DotNet 资源大全

    awesome-dotnet 是由 quozd 发起和维护.内容包括:编译器.压缩.应用框架.应用模板.加密.数据库.反编译.IDE.日志.风格指南等. https://github.com/jobb ...

随机推荐

  1. tips~function pointer

    An simple example: #include<stdio.h> int plus(int a,int b) { return a+b; } int main() { int (* ...

  2. STM32学习笔记&lpar;一&rpar; 如何新建一个STM32工程模板

    学习stm32,第一步就是选择开发工具了,GCC,MDK,IAR每一种都有自己的优劣势,这里我选择使用MDK软件实现STM32模板.当然如果想更快的接触stm32实例,领略嵌入式开发的魅力,STM也提 ...

  3. HTML中多媒体的应用&lowbar;Flash&sol;MP3&sol;设置可以活动的文字

    一.HTML中多媒体的应用_falsh动画(往网页中插入Flash动画) 1. Flash动画插入第一种方法:使用<embed>...</embed>标记动画会自动缩小 属性: ...

  4. IDF 实验室部分题目WriteUp

    前天花了一个下午的时间刷了几道IDF实验室的题目, 这个网站实在是有点冷清, 题目也比较少, 所以就被我和师兄们刷榜了2333... 因为我最先开始做, 所以就干脆刷到第一去了. 题目很水, 切莫见怪 ...

  5. 深入理解java的异常处理机制

     JAVA异常的概念    异常指不期而至的各种状况,如:文件找不到.网络连接失败.非法参数等.异常是一个事件,它发生在程序运行期间,干扰了正常的指令流程.Java通 过API中Throwable类的 ...

  6. cocos2d-x 通过JNI实现c&sol;c&plus;&plus;和Android的java层函数互调

    文章摘要: 本文主要实现两个功能: (1)通过Android sdk的API得到应用程序的包名(PackageName),然后传递给c++层函数. (2)通过c++函数调用Android的java层函 ...

  7. s2sh三大框架整合过程(仅供参考)

    三大框架顾名思义就是非常有名的Struts2 ,Hibernate,Spring, 框架整合的方法很多,现在我写一个非常简单的整合过程,相信大家一看就会! 这里使用的struts-2.2.1.1.hi ...

  8. JAVA课程设计——&OpenCurlyDoubleQuote;小羊吃蓝莓”小游戏

    JAVA课程设计--"小羊吃蓝莓"小游戏 1. 团队课程设计博客链接 http://www.cnblogs.com/HXY071/p/7061216.html 2. 个人负责模块或 ...

  9. python之线程相关的其他方法

    一.join方法 (1)开一个主线程 from threading import Thread,currentThread import time def walk(): print('%s is r ...

  10. 深入理解java虚拟机&lowbar;第二章&lowbar;读书笔记

    1.本章内容目录: 概述 运行时数据区域 程序计数器 java虚拟机栈 本地方法栈 java堆 方法区 运行时常量池 直接内存 HotSpot虚拟机对象探秘 对象的创建 对象的内存布局 对象的访问定位 ...