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

Model-View-ViewModel Pattern

Common abbreviations: M-V-VM or MVVM

Introduction

Separating user interface code from everything else is a key principle in well-engineered software.
在软件设计里 分隔 一个关键的原则

But it’s not always easy to follow and it leads to more abstraction in an application that is hard to understand.
但是 应用里 越多的抽象 很难理解

Quite a lot design patterns try to target this scenario: MVC, MVP, Supervising Controller, Passive View, PresentationModel, Model-View-ViewModel, etc.

很多设计模式 努力去实现 这些场景:MVC,MVP,管理控制器,被动视图,表示层模型,
The reason for this variety of patterns is that this problem domain is too big to be solved by one generic solution.
各种各样的模式的原因 是 通用型解决方案去解决问题太 困难 。

However, each UI Framework has its own unique characteristics and so they work better with some patterns than with others.
然而 每个 UI框架 有自己唯一的特征 ,所以 有些模式会比其他的更合适。

The WPF Application Framework (WAF) provides support for the Model-View-ViewModel Pattern because this one works best with Windows Presentation Foundation (WPF) applications.

WAF 支持 MVVM 模式 因为 它能在 微软推出的用户界面框架 表现很不错。

This pattern is also known as PresentationModel pattern.
这个模式被认为是 表示模型。

Definition

Represent the state and behavior of the presentation independently of the GUI controls used in the interface.
表述 状态 和 行为 表达 与 GUI 控件 无关的 接口

A popular description of this design pattern is done by Martin Fowler.
Martin Fowler 对 一个 流行 设计模式 的描述

In his article the pattern is called PresentationModel but it is the same pattern.
但是 相同的设计模式 在他的文章里 这个设计模式被叫做 表示 模型
You can read his article online on his website.
你可以在他的 网站 阅读他的文章。
The following chapters describe our specific .NET implementation of this design pattern.
我们特地用.net实现了文章里面描述的设计模式。

Structure

The following UML class diagram shows the collaborating classes of this design pattern in a logical layered architecture.

在 逻辑层 下面UML类图展示了 设计模式里各个类的协作。

Participants 参与者

The types participating in this pattern are:
View contains the specific GUI controls and defines the appearance of the user interface.
IView declares the interface of the View.
在模式里的参数类型有:
视图包含 特别的GUI控件 和 出现的 用户界面。

The ViewModel can communicate over this interface with the View.
ViewModel 通过 接口和 View 沟通。

Related pattern: Separated Interface (PoEA).
有关模式: 分开的接口

ViewModel represents the state and behavior of the presentation.

ViewModel 代表 展示层的 状态和行为

Model can be a business object from the domain layer or a service which provides the necessary data.

Model 是 业务对象 的领域层 或者 提供必要数据的服务层。

Controller is responsible for the workflow of the application. Furthermore, it mediates between the ViewModels.
控制器 对工作流应用程序负责。 此外, 他介于 ViewModels。

So it promotes loose coupling by keeping the ViewModels from referring to each other explicitly.
它可促进 涉及其他的明确的 保持 ViewModels 松散耦合

Related patterns: Application Controller (PoEA), Mediator (GoF)
相关模式: 应用控制器 ,中介者模式

Remarks 备注

The introduction of the interface IView is a variation of the Model-View-ViewModel pattern.
IView 是 Model-View-ViewModel 模式的变种。

It allows the ViewModel to call properties and methods on the View.
在view里 允许ViewModel调用 View里的属性和方法。

The Controller class is not part of this design pattern.
控制器不是设计模式的一部分。

The controller is supported by the WPF Application Framework (WAF) and so this article shows how the controller works together with the M-V-VM pattern.
控制器支持 WAF,所以这篇文章展示 控制器 怎么样和 MVVM模式一起工作。

Collaborations 协作

The View can call operations on the ViewModel directly.

View 能 直接地 回调 操作 ViewModel。

The ViewModel needs to communicate through the IView interface when it has to update the view.

当 它有更新 view , ViewModel 需要 通过 IView 接口 去 沟通。

The View can collaborate with the Model but it should restrict this on simple data binding.

View 能 和 Model 合作,但是 它 应该 约束这 在 简单的数据绑定。

It’s recommended to handle more complex operations by the ViewModel.
这被推荐 通过 ViewModel 去处理复杂的业务操作。

The upward communication from the Model to the View or to the ViewModel can be done through events.

通过事件, 从 Model到View或者ViewModel 是向上通信。
A common solution is to raise property changed events on the Model.
在model里 raise property changed 属性 事件是通用的解决办法
This way the View can use WPF data binding to synchronize the data between the Model and the View.
view使用WPF数据绑定 使得数据 在Model和View之间同步,
Related pattern: Observer (GoF).
观察者模式
The Controller can call operations on the ViewModel directly, whereas the backward communication from the ViewModel to 控制器通知 ViewModel 业务操作,ViewModel 通过事件去通知控制器。
the Controller might be done through events.

Related pattern: Observer (GoF).

Liabilities (缺点)

This specific implementation of the ViewModel pattern has the following liabilities:The ViewModel is dependent on a specific GUI framework.
实现 ViewModel模式伴随的确定:ViewModel 依赖 特定的 GUI 框架。

We use the commanding and the weak event concept of WPF.
我们用WPF里 指令 和 弱事件 概念。

The ViewModel might listen to events (e.g. PropertyChanged events) of the Model.
ViewModel 可能 去监听 Model 事件(举例 PropertyChanged 事件 )。

In such a scenario you have to keep the lifecycle of the ViewModel and the Model in mind.
是这样 一个场景 , 你 在心里 对 ViewModel和 Model 保持 周期。
When the Model lives longer as the ViewModel it is essential to unwire the event or to use weak events so that the garbage collector is able to remove the ViewModel instance.
当 Model 长时间活在ViewModel里 , 必须 取消事件 或者用 弱事件 ,垃圾回收可以清除ViewModel实例。
The WPF Application Framework (WAF) supports weak events with the ViewModel base class.

WAF 支持 弱事件 ViewModel。

Usage (用法)

The WPF Application Framework (WAF) provides some types which helps you to implement this pattern.

WPF 提供 一些 类型 帮你实现这个模式。

ViewModel class

Derive your ViewModel implementation from this class.
源自你的ViewModel实现这个类
The ViewModel class provides a default implementation of a weak event listener and it is responsible that the DataContext of the WPF view gets the instance of your ViewModel.

ViewModel 这个类提供 一个默认实现的 不牢固的事件 。它负责 WPF 数据上下文 获取 ViewModel 实例。
IView interface

All WPF views that are managed by a ViewModel have to implement this interface.
所有 WPF veiws 被 ViewModel 管理 去 实现这个接口。

You can create your own interface for exposing properties and methods of the View.
你可以创建 你自己的接口 去 实现 View里的属性和方法

每天翻译一点点: WPF Application Framework (WAF)的更多相关文章

  1. 使用WPF Application Framework (WAF)框架

    Visual Studio新建WAF项目的模板:https://marketplace.visualstudio.com/items?itemName=jbe2277.WAFProjectTempla ...

  2. Handling events in an MVVM WPF application

      Posted: June 30, 2013 | Filed under: MVVM, WPF, XAML |1 Comment In a WPF application that uses the ...

  3. OpenCASCADE Application Framework Data Framework Services

    OpenCASCADE Application Framework Data Framework Services eryar@163.com 一.概述Overview OpenCASCADE的数据框 ...

  4. OpenCascade Application Framework Introduction

    OpenCascade Application Framework Introduction eryar@163.com 本教程介绍了Open CASCADE程序框架(Application Fram ...

  5. ModSecurity web application firewall (WAF) Research

    catalog . 引言 . OWASP ModSecurity Core Rule Set (CRS) Project . Installation mod_security for Apache ...

  6. 【转】How to view word document in WPF application

    How to view word document in WPF application (CSVSTOViewWordInWPF) Introduction The Sample demonstra ...

  7. 侯老师的话(Application Framework)

    摘自http://blog.csdn.net/zlc19876/article/details/5355022 本篇文章主要介绍了"侯老师的话(Application Framework)& ...

  8. Merging a WPF application into a single EXE(WPF应用程序合并成单个Exe文件)

    I always dislike handing off little applications to people. Not because I can’t, but because of the ...

  9. 添加一个Application Framework Service

    如何添加一个Application Framework Service(without native code)? 1.本文参照AlarmManagerService实现一个简单的Applicatio ...

随机推荐

  1. es6笔记 day2---字符串模板及字符串新增

    字符串连接案例 注意:引号变了,为键盘数字1旁边的飘花键 以前的老写法是在字符串中加入“+”号,给几个字符串给串起来,那种写法是要死人的. 现在只需加上一对``即可将字符串连接起来 --------- ...

  2. CodeForces 1204 (#581 div 2)

    传送门 A.BowWow and the Timetable •题意 给你一个二进制数,让你求小于这个数的所有4的幂的个数 •思路 第一反应是二进制与四进制转换 (其实不用真正的转换 QwQ) 由于二 ...

  3. LuoguP3066 逃跑的BarnRunning Away From…

    LuoguP3066 先吐槽一下,这道题名字好长啊 一个非常明显的思路,利用倍增数组不断向上跳.直到数值大于\(L\),然后直接差分统计答案就好了. 这种ZROI也考过,不多赘述了. 我们来考虑主席树 ...

  4. UVA - 10480 Sabotage (Dinic)

    The regime of a small but wealthy dictatorship has been abruptly overthrown by an unexpected rebel-l ...

  5. javascript拷贝

    function copy(obj){ //浅拷贝 var result = {}; for(var attr in obj){ result[attr] = obj[attr]; } return ...

  6. windows下的redis和redismyadmin

    redis默认是16个数据库,从0-15 由于项目需要,我使用了19号数据库,然而再向19号数据库添加数据的时候,通过redismyadmin查看发现添加到19号数据库的数据会同步到0,16,17,1 ...

  7. Java并发编程系列-(8) JMM和底层实现原理

    8. JMM和底层实现原理 8.1 线程间的通信与同步 线程之间的通信 线程的通信是指线程之间以何种机制来交换信息.在编程中,线程之间的通信机制有两种,共享内存和消息传递. 在共享内存的并发模型里,线 ...

  8. 雪花算法 Snowflake & Sonyflake

    唯一ID算法Snowflake相信大家都不墨生,他是Twitter公司提出来的算法.非常广泛的应用在各种业务系统里.也因为Snowflake的灵活性和缺点,对他的改造层出不穷,比百度的UidGener ...

  9. 开发板免费领!腾讯云IoT应用创新大赛正式启动!

    大赛简介 腾讯云IoT应用创新大赛是腾讯云面向物联网领域举办的大型竞赛,通过腾讯云IoT全链路产品能力,开放平台和服务,与广大开发者共同创新,孵化优秀的IoT产品和解决方案,共同构建IoT应用生态. ...

  10. 0002 认识HTML(骨架、DOCTYPE、lang、charset)

    学习目标 理解 1.HTML的概念 2.HTML标签的分类 3.HTML标签的关系 4.HTML标签的语义化 应用 1.HTML骨架格式 2.sublime基本使用 1. HTML 初识 HTML 指 ...