MVVM

The model-view-viewmodel is a typically WPF pattern. It consists of a view that gets all the user input and forwards it to the viewmodel, typically by using commands. The view actively pulls the data from the viewmodel by using data binding. The model doesn’t know about the ViewModel and the ViewModel doesn’t know about the View.

(MVVM是典型的WPF设计模式。

view通过命令将全部用户的输入传递给viewmodel。

view通过数据绑定从viewmodel中获得数据。model不了解viewmodel的情况,viewmodel不了解view的情况)

Explaining MVVM through an example

Let’s create a sample Patient Management Application using MVVM. Going step by step we must first define our Model, then define the ViewModel and finally the View.

(通过一个样例来解释MVVM

让我使用MVVM创建一个简单的患者管理应用程序。按部就班我们必须首先定义Model(模式),然后定义ViewModel。最后定义View。)

Step 1 - Creating the Model:

(第一步:创建Model)

Def: The Model is often referred to as Domain Model, completely UI independent that stores the state and helps in the processing of the problem domain. In our example Model becomes the DOM object which is nothing but the patient entity. The sample below shows the patient Entity.

(Model通常指的是领域模型。存储了全然独立于UI的状态,帮助问题的处理。)

public class Patient
{
public int Id { get; set; }
public string Name { get; set; }
public Int64 MobileNumber { get; set; }
}

Step 2 – Creating the View Model

(第二步:创建ViewModel)

Def : The term means “Model of a View”, and can be thought of as abstraction of the view, but it also provides a specialization of the Model that the View can use for data-binding. In this latter role the ViewModel contains data-transformers that convert Model types into View types, and it contains Commands the View can use to interact with the Model. The ViewModel has been likened to a conceptual state of the data as opposed to the real state of the data in the Model.

(字面上看,ViewModel的意思是“视图的模型”。也能够被觉得是view的抽象,可是它也是一个特殊化的模式,而且其view能够使用数据绑定。最新的角色。ViewModel包括了一个数据传送器,即将Model中的数据传送到View中。

ViewModel中相同包括了一些命令,这样能够使得View能够与Model进行交互。)

Coming back to our example lets create the PatientViewModel which will expose the Properties(which contains the Data) and Commands (User actions) which will be binded to the view.

(回到我们的样例,让我们创建PatientViewModel,包括了内容和命令。)

Commands :

Def : The main idea is to move the whole presentation logic to the view model. This can be attained by using another feature of WPF, namely Commands. Commands can be bound like data and are supported by many elements as buttons, toggle buttons, menu items, checkboxes and input bindings. The goal here is not to have any line of logic in the code-behind of a view.

The View will execute commands on user action and the commands will be implemented in the ViewModel. To do this, you can’t use the standard WPF RoutedUIEvent, but you can easily develop your own command classes. A common way to do this is to create a command object that calls a delegate you specify.

Here is an example of how you can define a custom command class which you will later use when you are defining the commands in the ViewModel.

(我们基本的想法是把全部的逻辑展示放到ViewModel中。

这样能够使用WPF的还有一个特点:命令。Command能够向数据一样被约束,同一时候能够被非常多种元素支持,比方按钮、三态按钮、菜单栏目、复选框等。

View针对用户的动作运行命令,而且这些命令会运行于ViewModel中。

为了这样奏效,我们不能使用WPF中标准的路由事件。可是我们能够轻松开发自己的command类。

相同的方法就是创建一个command对象来调用你指定的托付。

这个样例教你怎样定义一个通用的command类,这个类在稍后你定义ViewModel中commands的时候使用。

public class RelayCommand : ICommand
{
readonly Action _execute;
readonly Predicate _canExecute;
public RelayCommand(Action execute)
: this(execute, null){}
public RelayCommand(Action execute,
Predicate canExecute)
{
if (execute == null)
throw new ArgumentNullException("execute");
_execute = execute;
_canExecute = canExecute;
}
public bool CanExecute(object parameter)
{
return _canExecute == null ? true : _canExecute(parameter);
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public void Execute(object parameter)
{
_execute(parameter);
}
}

Coming back to our example, our sample app requires Add, Delete & Search functionalities. The sample shows how to implement Add command using the RelayCommand that we defined above. (回到我们的样例,我们这个简单的APP须要加入、删除、查找等功能。以加入命令为例,我们将演示怎样使用我们上面定义的RelayCommand。)

public class PatientDetailViewModel: INotifyPropertyChanged
{
public ICommand AddPatientCmd
{
get { return _addPatientCmd; }
}
public PatientDetailViewModel()
{
_addPatientCmd = new RelayCommand(Add, CanAdd);
}
public bool CanAdd(object obj) { //Logic }
public void Add(object obj) { //Logic }
}

Step 3 – Creating the View

(第二步:创建View)

Def : A View is defined in XAML and should not have any logic in the code-behind. It binds to the view-model by only using data binding. The view involves mostly just setting up the UI and bindings to the ViewModel. The DataContext property for this control will be set to the ViewModel. As we have created the PatientViewModel all we need to do is create the PatientDetailView and then bind the ViewModel as the DataContext. The Sample here shows Property binding and Command binding.

(View在MAML中定义的,而且在后台代码中,View不应该含有不论什么的逻辑。它只通过数据绑定将view绑定到viewmodel。)

<UserControl ...>
<!--Bind the View Model to the View. -->
<UserControl.DataContext>
<ViewModels:PatientDetailViewModel/>
</UserControl.DataContext>
....
<TextBox Name="TbxName"
....
Text="{Binding Path=Name}"/>
....
<Button Name="BtnAdd" Content="Add"
....
Command="{Binding AddPatientCmd}"/>
.....
</UserControl>

WPF-MVVM-Demo的更多相关文章

  1. 使用Prism提供的类实现WPF MVVM点餐Demo

    使用Prism提供的类实现WPF MVVM点餐Demo 由于公司开发的技术需求,近期在学习MVVM模式开发WPF应用程序.进过一段时间的学习,感受到:学习MVVM模式,最好的方法就是用MVVM做几个D ...

  2. WPF MVVM 验证

    WPF MVVM(Caliburn.Micro) 数据验证 书接前文 前文中仅是WPF验证中的一种,我们暂且称之为View端的验证(因为其验证规是写在Xaml文件中的). 还有一种我们称之为Model ...

  3. WPF自学入门(十一)WPF MVVM模式Command命令

    在WPF自学入门(十)WPF MVVM简单介绍中的示例似乎运行起来没有什么问题,也可以进行更新.但是这并不是我们使用MVVM的正确方式.正如上一篇文章中在开始说的,MVVM的目的是为了最大限度地降低了 ...

  4. 定时任务 Wpf.Quartz.Demo.2

    定时任务 Wpf.Quartz.Demo.1已经能运行了,本节开始用wpf搭界面. 准备工作: 1.界面选择MahApp.Metro 在App.xaml添加资源 <Application.Res ...

  5. WPF MVVM(Caliburn.Micro) 数据验证

    书接前文 前文中仅是WPF验证中的一种,我们暂且称之为View端的验证(因为其验证规是写在Xaml文件中的). 还有一种我们称之为Model端验证,Model通过继承IDataErrorInfo接口来 ...

  6. WPF自学入门(十一)WPF MVVM模式Command命令 WPF自学入门(十)WPF MVVM简单介绍

    WPF自学入门(十一)WPF MVVM模式Command命令   在WPF自学入门(十)WPF MVVM简单介绍中的示例似乎运行起来没有什么问题,也可以进行更新.但是这并不是我们使用MVVM的正确方式 ...

  7. .NET Core 3 WPF MVVM框架 Prism系列之事件聚合器

    本文将介绍如何在.NET Core3环境下使用MVVM框架Prism的使用事件聚合器实现模块间的通信 一.事件聚合器  在上一篇 .NET Core 3 WPF MVVM框架 Prism系列之模块化 ...

  8. .NET Core 3 WPF MVVM框架 Prism系列之对话框服务

     本文将介绍如何在.NET Core3环境下使用MVVM框架Prism的对话框服务,这也是prism系列的最后一篇完结文章,下面是Prism系列文章的索引: .NET Core 3 WPF MVVM框 ...

  9. WPF MVVM初体验

    首先MVVM设计模式的结构, Views: 由Window/Page/UserControl等构成,通过DataBinding与ViewModels建立关联: ViewModels:由一组命令,可以绑 ...

  10. WPF MVVM实现TreeView

    今天有点时间,做个小例子WPF MVVM 实现TreeView 只是一个思路大家可以自由扩展 文章最后给出了源码下载地址 图1   图2     模版加上了一个checkbox,选中父类的checkb ...

随机推荐

  1. javascript try{}catch(e){}

    <script language="javascript"> try {  throw new Error(10,"asdasdasd") } ca ...

  2. 浅析Java抽象类和接口的比較

    abstract class和interface是Java语言中对于抽象类定义进行支持的两种机制,正是因为这两种机制的存在,才赋予了Java强大的面向对象能力. abstract class和inte ...

  3. React-Native系列Android——Native与Javascript通信原理(一)

    React-Native最核心的是Native与Javascript之间的通信,并且是双向通信.Native层到Javascript层,Javascript层到Native层.虽说是两个方向,但实现上 ...

  4. 一个软件实现的Linux看门狗—soft_wdt

    soft_wdt(下面简称本软件)是一个软件实现的Linux看门狗. 本软件是一款开源.免费软件. 下载地址: https://github.com/sunmingbao/soft-wdt/archi ...

  5. moveToThread的根本目的还是为了处理QObject的事件循环(如果为空指针的话,当前对象的所有的事件都不处理了),看官方说明就知道了

    Changes the thread affinity for this object and its children. The object cannot be moved if it has a ...

  6. [luogu P3813] [FJOI2017] 矩阵填数 解题报告 (容斥原理)

    题目链接: https://www.luogu.org/problemnew/show/P3813 题目: 给定一个 h*w的矩阵,矩阵的行编号从上到下依次为 1..h,列编号从左到右依次1..w. ...

  7. Kettle学习系列之Kettle能做什么?(三)

    不多说,直接上干货! PDI(Kettle) 都能做什么? 可以说凡是有数据整合.转换.迁移的场景都可以使用PDI,他代替了完成数据转换任务的手工编码,降低了开发难度. 同时,我们可以在自己实际业务里 ...

  8. SparkSession - Spark SQL 的 入口

    SparkSession - Spark SQL 的 入口 翻译自:https://jaceklaskowski.gitbooks.io/mastering-apache-spark/content/ ...

  9. WPF 样式

    样式是属性值的集合,能被应用到一个元素,类似CSS,每个控件最多只能有一个样式,通过控件的Stype属性应用样式,如下代码,其中BigFontButtonStyle是用于检索资源的关键字,也可以通过代 ...

  10. 乌班图 之 设置镜像服务器 、设置屏幕分辨率QAQ

    设置镜像服务器 Ubuntu 中的大部分软件安装都是用apt命令,从Ubuntu的服务器上直接安装的. 但是国外你懂的网速是硬伤,因此要搞个镜像服务器,内容当然都是一样的咯. 第一步:进入系统设置 第 ...