先科普一下:什么是WPF,请看下图

微软对于WPF技术的构想是很宏大的,可惜普及率不高,不过如果你要做Windows客户端开发的话WPF技术还是值得一学的。

什么是MVVM模式#

简单来说它是一种高级的UI设计模式。据我所知目前还运用在一些js框架中,比如AngularJS。其他的UI设计模式还包括MVC、MVP,个人觉得最强大的还是MVVM。

MVVM主体框架如下图:

  • The Model is the entity that represents the business concept; it can be anything from a simple customer entity to a complex stock trade entity .
  • The View is the graphical control or set of controls responsible for rendering the Model data on screen .A View can be a WPF window, a Silverlight page, or just an XAML data template control .
  • The ViewModel is the magic behind everything .The ViewModel contains the UI logic, the commands, the events, and a reference to the Model .
  • In MVVM, the ViewModel is not in charge of updating the data displayed in the UI—thanks to the powerful data-binding engine provided by WPF and Silverlight, the ViewModel doesn’t need to do that .This is because the View is an observer of the ViewModel, so as soon as the ViewModel changes, the UI updates itself .For that to happen, the ViewModel must implement the INotifyPropertyChangedinterface and fire the PropertyChangedevent .

简单翻译一下(不全)

  • The Model 代表业务逻辑的实体类,可以是一个简单的顾客实体类,也可以是一个复杂的股票交易实体类。
  • The View 代表一个用户界面控件 ...
  • The ViewModel 包括各种逻辑、命令、事件以及实体类的引用。

什么是MVVMFoundation#

MVVMFoundation是一个最简单的MVVM框架,官方介绍如下:

MVVM Foundation is a library of classes that are very useful when building applications based on the Model-View-ViewModel philosophy. The library is small and concentrated on providing only the most indispensable tools needed by most MVVM application developers

MVVMFoundation包含四大模块:

  • ObservableObject:这里相当于ViewModelBase的概念,每一个ViewModel继承自该类,调用完成之后立即释放,防止内存泄露。

  • RelayCommand接口:封装command的声明,包括execution执行逻辑,可选的can-execute逻辑等。外部只需要实例化并Binding就可以简单使用。

  • Messenger:这里主要用在各种不同的ViewModel之间通信(比如相互关联的ViewModel、主从ViewModel等),当然也可以扩展成ViewModel与View之间进行通信。

  • PropertyObserver:主要是对INotifyPropertyChanged.PropertyChanged进行封装,可以通过其对某个对象的属性变更注册回调函数,当属性变更时便触发回调函数。

ObservableObject#

实现ViewModel中的属性改变通知到绑定的控件的方法,相当于是所有Viewmodel的基类。

  1. 使用时调用OnPropertyChange方法,则后台数据变化即可通知界面刷新

     public string UserName
    {
    get { return this.user.UserName; }
    set
    {
    this.user.UserName = value;
    OnPropertyChanged("UserName");
    }
    }
  2. 属性在View界面的绑定

     <TextBox Text="{Binding UserName}" />

RelayCommand#

用于在ViewModel中定义View中绑定的命令,代替了以前Winform的Click事件。

  1. 在ViewModel中定义Command

     public ICommand BrowseImageCommand
    {
    get { return new ICommand(BrowseImage); }
    } private void BrowseImage()
    {
    ...
    }
  2. 在View中的按钮关联此Command

     <Button Content="浏览..." Command="{Binding BrowseImageCommand}"/>

Messenger#

可用于ViewModel之间的信息传递,可以用于ViewModel和View之间的信息传递。

  1. 定义信息传输类

     public class ViewModelCommunication
    {
    static ViewModelCommunication()
    {
    Messaging = new Messenger();
    } public static Messenger Messaging { get; set; } public static string DataIDInChanged { get { return "DataIDInChanged"; } }
    }
  2. 在需要通知的类中注册要通知的信息

     ViewModelCommunication.Messaging.Register(ViewModelCommunication.DataIDInChanged,(Action<string>)(param => SetLastSelectedDataID(param)));
  3. 当对应的消息出现时,通知已经注册的类

     ViewModelCommunication.Messaging.NotifyColleagues(ViewModelCommunication.DataIDInChanged, DataID.ToString());

PropertyObserver#

主要用于对对象的属性监听,属性变更后可触发已注册的回调函数。

  1. 注册要监听对象的属性及回调函数

     PropertyObserver<UserInfoViewModel> userInfoAfterObserver;
    public MainWindowViewModel()
    {
    UserInfoBefore = new UserInfoViewModel();
    userInfoAfterObserver = new PropertyObserver<UserInfoViewModel>(UserInfoAfter)
    .RegisterHandler(UserInfo => UserInfo.Age, this.AgeChangedCallback);
    }
  2. 实现回调函数

     private void AgeChangedCallback(UserInfoViewModel userInfo)
    {
    MessageBox.Show("Property Age changed");
    }

以上就是MVVMFoundation框架的主要使用方法,感兴趣的人可以用用看~欢迎留言交流心得~

浅谈WPF中的MVVM框架--MVVMFoundation的更多相关文章

  1. 浅谈WPF中对控件的位图特效(WPF Bitmap Effects)

    原文:浅谈WPF中对控件的位图特效(WPF Bitmap Effects) -------------------------------------------------------------- ...

  2. 简单的介绍下WPF中的MVVM框架

    最近在研究学习Swift,苹果希望它迅速取代复杂的Objective-C开发,引发了一大堆热潮去学它,放眼望去各个培训机构都已打着Swift开发0基础快速上手的招牌了.不过我觉得,等同于无C++基础上 ...

  3. 浅谈前端中的mvvm与mvc

    用了vue这么久,却没有认真的关注mvvm与mvc,着实汗颜.趁着周末刚好看了一下网上的文章还有书籍,简单的谈一下我的理解. -以下图片均摘自网络. 一.MVC 特点:单项通讯 视图(View):用户 ...

  4. 浅谈WPF中的PreviewTextInput

    今天在使用TextBox的TextInput事件的时候,发现无论如何都不能触发该事件,然后百思不得其解,最后在MSDN上找到了答案:TextInput 事件可能已被标记为由复合控件的内部实现进行处理. ...

  5. 由项目浅谈JS中MVVM模式

    文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/. 1.    背景 最近项目原因使用了durandal.js和knock ...

  6. js架构设计模式——由项目浅谈JS中MVVM模式

    1.    背景 最近项目原因使用了durandal.js和knockout.js,颇有受益.决定写一个比较浅显的总结. 之前一直在用SpringMVC框架写后台,前台是用JSP+JS+标签库,算是很 ...

  7. 浅谈WPF页间导航

    浅谈WPF页间导航 使用导航的目的是从一个页面进入到另一个页面.无论是预先决定的线性顺序(向导)还是基于层次的用户驱动程序(大部分网站的形式),或者动态生成的路径,主要有3种方法实现:调用Naviga ...

  8. 浅谈WPF依赖项属性

    浅谈WPF依赖项属性 0. 引言 依赖项属性虽然在使用上和CLR属性一样,但是它是WPF特有的,不同于CLR属性.只是封装为我们常用CLR的属性,在语法使用上和CLR属性一样.WPF中一些功能:动画, ...

  9. 浅谈HTTP中GET、POST用法以及它们的区别

    浅谈HTTP中GET.POST用法以及它们的区别 HTTP定义了与服务器交互的不同方法,最基本的方法有4种,分别是GET,POST,PUT,DELETE.URL全称是资源描述符.我们可以这样认为: 一 ...

随机推荐

  1. Catalan 数列的性质及其应用(转载)

    转自:http://lanqi.org/skills/10939/ 卡特兰数 — 计数的映射方法的伟大胜利 发表于2015年11月8日由意琦行 卡特兰(Catalan)数来源于卡特兰解决凸$n+2$边 ...

  2. VB输出数据到EXCEL

    Private Sub Command1_Click() Dim i As Long Dim j As Long , ) As Long Dim xlApp, WS, WB Set xlApp = C ...

  3. firewalld的基本使用

    参考原文链接:https://www.cnblogs.com/moxiaoan/p/5683743.html 1.firewalld的基本使用 启动: systemctl start firewall ...

  4. Linux_常用命令简单介绍(netstat,awk,top,tail,head,less,more,cat,nl)

    1.netstat netstat -tnl | grep 443 (查看443端口是否被占用) root用户,用netstat -pnl | grep 443 (还可显示出占用本机443端口的进程P ...

  5. MangoDb的安装及使用

    安装步骤 一.创建文件 vi /etc/yum.repos.d/mongodb-org-3.6.repo 二.配置文件内容 [mongodb-org-3.6] name=MongoDB Reposit ...

  6. CannyLab/tsne-cuda with cuda-10.0

    t-SNE-CUDA Barnes-Hut t-SNE https://github.com/CannyLab/tsne-cuda/projects 做数据降维时常用到,但计算较慢,所以可用cuda加 ...

  7. redhat 6 红帽6 Linux 网络配置

    [root@ORAMYSQL ~]# cd /etc/sysconfig/network-scripts/ 切换到网卡配置目录 [root@ORAMYSQL network-scripts]# vi ...

  8. url中文参数乱码问题

    1.参数乱码: js: var url = $$pageContextPath + "iecp/ads/heilanAnalogCurve.do?pointCode=" + get ...

  9. Hadoop 学生平均成绩

    1.实例描述 通过一个计算学生平均成绩的例子来讲解开发MapReduce程序的流程.输入文件都是纯文本文件,输入文件中的每行内容均为一个学生的姓名和他相应的成绩,如果有多门学科,则每门学科为一个文件. ...

  10. [Swift]LeetCode90. 子集 II | Subsets II

    Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...