技术笔记:WindowsPhoneMango:MVVM十分钟入门

简介: 技术笔记:WindowsPhoneMango:MVVM十分钟入门

在这篇文章中,我将谈一下在windows phone 7.1 Mango应用程序中使用MVVM设计模式。用较少的理论、更多的示例,在10分钟内来解释MVVM模式。


在开始之前,先简短介绍一下什么是MVVM:Model-View-ViewModel (MVVM)模式提供了一种灵活的方式,通过将应用程序分隔成三部分来创建windows phone应用程序。


a:View:放置XAML文件。


b:ViewModel:放置连接UI和数据的显示逻辑层。


c:放置数据模型和业务对象。


有关更多信息,看参阅官方MSDN文档。


为什么选择MVVM?有什么好处?


视图和显示逻辑层分离:从显示逻辑层分离出来View/XAML,可以使开发者只关注Code,而设计人员只关注XAML。


自动单元测试:视图/逻辑分离大大改进了表现逻辑层的自动单元测试。


代码重用:因为显示逻辑层在独立的组件或类中,和View(XAML)分离开来,你可以按照你喜欢的方式,通过继承和组合使它们结合起来。


设计时数据支持: 你可以在Blend中看到UI的样子,也就是说,设计人员可以使用样本数据来测试UI,甚至可以模拟实际场景数据。


多视图: 依据用户角色,同一ViewModel可以在不同的多视图中展现。


入门开始:


我们需要Windows Phone 7.1 Mango工程项目。在这个实例中,我们会使用上篇文章 为Windows Phone Mango MVVM 应用创建可复用 ICommand 实现类 所创建的DelegateCommand。请注意:commanding是伴随mango更新的新特性。(commanding意味着一些控件不支持Commands)。


MODEL


首先我们需要定义Model。在示例中,我们创建Person类,有两个属性:Name和Age。在这里,最重要的就是要实现INotifyPropertyChanged接口,因为,当Person对象发现变化时,需要通知给UI。(本例中,当按下Save Changes时,更新ListBox,更多信息参阅下面部分中的View)


using System;


using System.Net;


using System.Windows;


using System.Windows.Controls;


using System.Windows.Documents;


using System.Windows.Ink;


using System.Windows.Input;


using System.Windows.Media;


using System.Windows.Media.Animation;


using System.Windows.Shapes;


using System.ComponentModel;


namespace WPMangoMVVMSample


{


public class Person : INotifyPropertyChanged


{


private string name;


private int age;


public string Name


{


get


{


return name;


}


set


{


if (this.name != value)


{


this.name = value;


this.RaisePropertyChanged("Name");


}


}


}


public int Age


{


get


{


return this.age;


}


set


{


if (this.age != value)


{


this.age = value;


this.RaisePropertyChanged("Age");


}


}


}


public event PropertyChangedEventHandler PropertyChanged;


private void RaisePropertyChanged(string propertyName)


{


PropertyChangedEventHandler handler = this.PropertyChanged;


if (handler != null)


{


handler(this, new PropertyChangedEventArgs(propertyName));


}


}


}


}


.csharpcode, .csharpcode pre { font-size: small; color: rgba(0, 0, 0, 1); font-family: consolas, "Courier New", courier, monospace; background-color: rgba(255, 255, 255, 1) }


.csharpcode pre { margin: 0 }


.csharpcode .rem { color: rgba(0, 128, 0, 1) }


.csharpcode .kwrd { color: rgba(0, 0, 255, 1) }


.csharpcode .str { color: rgba(0, 96, 128, 1) }


.csharpcode .op { color: rgba(0, 0, 192, 1) }


.csharpcode .preproc { color: rgba(204, 102, 51, 1) }


.csharpcode .asp { background-color: rgba(255, 255, 0, 1) }


.csharpcode .html { color: rgba(128, 0, 0, 1) }


.csharpcode .attr { color: rgba(255, 0, 0, 1) }


.csharpcode .alt { background-color: rgba(244, 244, 244, 1); width: 100%; margin: 0 }


.csharpcode .lnum { color: rgba(96, 96, 96, 1) }


VIEW MODEL


下一步就是创建PersonViewModel,主要包含以下部分:


SaveChangesCommand - 保存用户选择Person对象所引发的修改。


LoadDataCommand - 此命令是用来填充ObservableCollection(Person对象)。


SelectedName - 此属性表示选中的Person对象的名称。


SelectedAge - 此属性表示选中的Person对象的年龄。


SelectedPerson - 此属性表示所选中的Person对象。


PersonViewModel也实现了INotifyPropertyChanged,所有当某些属性更新时,UI会接到通知。 代码如下:


using System;


using System.Net;


using System.Windows;


using System.Windows.Controls;


using System.Windows.Documents;


using System.Windows.Ink;


using System.Windows.Input;


using System.Windows.Media;


using System.Windows.Media.Animation;


using System.Windows.Shapes;


using System.ComponentModel;


using System.Collections.ObjectModel;


namespace WPMangoMVVMSample


{


public class PersonViewModel : INotifyPropertyChanged


{


private string name;


private int age;


private ObservableCollection


personDataSource;


private ICommand loadDataCommand;


private ICommand saveChangesCommand;


public PersonViewModel()


{


this.loadDataCommand = new DelegateCommand(this.LoadDataAction);


this.saveChangesCommand = new DelegateCommand(this.SaveChangesAction);


}


private void LoadDataAction(object p)


{


this.DataSource.Add(new Person() { Name = "John", Age = 32 });


this.DataSource.Add(new Person() { Name = "Kate", Age = 27 });


this.DataSource.Add(new Person() { Name = "Sam", Age = 30 });


}


private void SaveChangesAction(object p)


{


if (this.SelectedPerson != null)


{


this.SelectedPerson.Name = this.name;


this.SelectedPerson.Age = this.age;


}


}


public ICommand LoadDataCommand


{


get


{


return this.loadDataCommand;


}


}


public ICommand SaveChangesCommand


{


get


{


return this.saveChangesCommand;


}


}


public ObservableCollection


DataSource


{


get


{


if (this.personDataSource == null)


{


this.personDataSource = new ObservableCollection


();


}


return this.personDataSource;


}


}


public string SelectedName


{


get


//代码效果参考:http://www.lyjsj.net.cn/wx/art_23076.html

{

if (this.SelectedPerson != null)


{


return this.SelectedPerson.Name;


}


return string.Empty;


}


set


{


this.name = value;


}


}


public int SelectedAge


{


get


{


if (this.SelectedPerson != null)


{


return this.SelectedPerson.Age;


}


return 0;


}


set


{


this.age = value;


}


}


private Person selectedPerson;


public Person SelectedPerson


{


get


{


return this.selectedPerson;


}


set


{


if (this.selectedPerson != //代码效果参考:http://www.lyjsj.net.cn/wx/art_23074.html

value)

{


this.selectedPerson = value;


if (this.selectedPerson != null)


{


this.name = this.selectedPerson.Name;


this.age = this.selectedPerson.Age;


}


this.RaisePropertyChanged("SelectedName");


this.RaisePropertyChanged("SelectedAge");


}


}


}


public event PropertyChangedEventHandler PropertyChanged;


private void RaisePropertyChanged(string propertyName)


{


PropertyChangedEventHandler handler = this.PropertyChanged;


if (handler != null)


{


handler(this, new PropertyChangedEventArgs(propertyName));


}


}


}


}


.csharpcode, .csharpcode pre { font-size: small; color: rgba(0, 0, 0, 1); font-family: consolas, "Courier New", courier, monospace; background-color: rgba(255, 255, 255, 1) }


//代码效果参考: http://www.lyjsj.net.cn/wz/art_23072.html

.csharpcode pre { margin: 0 }


.csharpcode .rem { color: rgba(0, 128, 0, 1) }


.csharpcode .kwrd { color: rgba(0, 0, 255, 1) }


.csharpcode .str { color: rgba(0, 96, 128, 1) }


.csharpcode .op { color: rgba(0, 0, 192, 1) }


.csharpcode .preproc { color: rgba(204, 102, 51, 1) }


.csharpcode .asp { background-color: rgba(255, 255, 0, 1) }


.csharpcode .html { color: rgba(128, 0, 0, 1) }


.csharpcode .attr { color: rgba(255, 0, 0, 1) }


.csharpcode .alt { background-color: rgba(244, 244, 244, 1); width: 100%; margin: 0 }


.csharpcode .lnum { color: rgba(96, 96, 96, 1) }


VIEW


最后一步就是创建View。 我们仍然使用 MainPage.xaml。我们将创建:


"LoadData" 按钮: 调用LoadDataCommand填充数据源。


ListBox 绑定PersonViewModel,两个TextBox用来编辑选择的Person对象: 姓名和年龄。


"Save Changes" 调用SaveChangesCommand保存修改信息。


注意: TextBox的Binding 数据流向:Mode=TwoWay ,可以编辑和更新值。


[/span>StackPanel x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"

[/span>Button Content="LoadData" Command="{Binding LoadDataCommand}" />


[/span>ListBox ItemsSource="{Binding DataSource}" SelectedItem="{Binding SelectedPerson, Mode=TwoWay}" Height</span>="100"

[/span>ListBox.ItemTemplate

[/span>DataTemplate

[/span>StackPanel Orientation="Horizontal"

[/span>TextBlock Text="Name:"/>


[/span>TextBlock Text="{Binding Name}" />


[/span>TextBlock Text="Age:" Margin="10,0,0,0"/>


[/span>TextBlock Text="{Binding Age}" />






[/span>TextBlock Text="Name:"/>


[/span>TextBox Text="{Binding SelectedName, Mode=TwoWay}" />


[/span>TextBlock Text="Age:"/>


[/span>TextBox Text="{Binding SelectedAge, Mode=TwoWay}" />


[/span>Button Content="Save Changes" Command="{Binding SaveChangesCommand}" />



.csharpcode, .csharpcode pre { font-size: small; color: rgba(0, 0, 0, 1); font-family: consolas, "Courier New", courier, monospace; background-color: rgba(255, 255, 255, 1) }


.csharpcode pre { margin: 0 }


.csharpcode .rem { color: rgba(0, 128, 0, 1) }


.csharpcode .kwrd { color: rgba(0, 0, 255, 1) }


.csharpcode .str { color: rgba(0, 96, 128, 1) }


.csharpcode .op { color: rgba(0, 0, 192, 1) }


.csharpcode .preproc { color: rgba(204, 102, 51, 1) }


.csharpcode .asp { background-color: rgba(255, 255, 0, 1) }


.csharpcode .html { color: rgba(128, 0, 0, 1) }


.csharpcode .attr { color: rgba(255, 0, 0, 1) }


.csharpcode .alt { background-color: rgba(244, 244, 244, 1); width: 100%; margin: 0 }


.csharpcode .lnum { color: rgba(96, 96, 96, 1) }


绑定view至view model简单方法就是设置DataContext:


public MainPage()


{


InitializeComponent();


// simple way to bind the view to the view model


this.DataContext = new PersonViewModel();


}


.csharpcode, .csharpcode pre { font-size: small; color: rgba(0, 0, 0, 1); font-family: consolas, "Courier New", courier, monospace; background-color: rgba(255, 255, 255, 1) }


.csharpcode pre { margin: 0 }


.csharpcode .rem { color: rgba(0, 128, 0, 1) }


.csharpcode .kwrd { color: rgba(0, 0, 255, 1) }


.csharpcode .str { color: rgba(0, 96, 128, 1) }


.csharpcode .op { color: rgba(0, 0, 192, 1) }


.csharpcode .preproc { color: rgba(204, 102, 51, 1) }


.csharpcode .asp { background-color: rgba(255, 255, 0, 1) }


.csharpcode .html { color: rgba(128, 0, 0, 1) }


.csharpcode .attr { color: rgba(255, 0, 0, 1) }


.csharpcode .alt { background-color: rgba(244, 244, 244, 1); width: 100%; margin: 0 }


.csharpcode .lnum { color: rgba(96, 96, 96, 1) }


效果图:


源代码下载


译自:windowsphonegeek


由于本人翻译水平有限,有些地方欠妥,请园友们不吝指教!

相关文章
|
移动开发 编解码 小程序
(H5小程序页面html)页面跳转参数丢失
在 URL 参数字符串中用 key=value 这种键值对的形式进行传递参数,多个键值对中间用 & 连接。如果在 value 中也存在 & 这个符号的话,不对其进行编码,就会引起歧义,就会发生参数丢失问题。
1153 0
|
12月前
|
JSON 应用服务中间件 nginx
采集 Nginx 日志的几种方式
本文系统介绍采集Nginx日志的六种主流方式:本地文件读取、Agent采集(如Filebeat)、Syslog转发、Sidecar模式、JSON格式化输出及云服务集成。涵盖单机到云原生场景,助你构建高效、可扩展的日志体系,提升监控与故障排查能力。(238字)
805 152
|
Shell
Gitee 如何自动部署 Pages?还是用 GitHub Actions!
在上篇《一篇教你代码同步 GitHub 和 Gitee》中,我们使用 GitHub Actions 解决了 GitHub 代码自动同步 Gitee 的问题,但我们的博客仓库代码同步到 Gitee 后,并不能像 GitHub 一样自动部署 Pages,如果不使用付费的 Gitee Pages Pro 服务,那我们该怎么实现 Gitee 自动部署 Pages 呢?
1397 0
Gitee 如何自动部署 Pages?还是用 GitHub Actions!
|
数据采集 监控 测试技术
2025年住宅代理IP服务商TOP10榜单最新出炉
2025年主流住宅代理IP服务商盘点:SmartProxy凭高性价比和易用性脱颖而出,适合中小团队;NetNut、Bright Data、Oxylabs等各具优势,涵盖SEO、电商、广告监控等场景。
|
开发工具 异构计算 Perl
ZYNQ_SDK EMIO
ZYNQ_SDK EMIO
|
Linux 网络安全 开发工具
Git学习笔记(一):基础与应用
本文档详细介绍了如何将本地项目关联到Gitee上的空仓库并上传代码,以及如何验证本机与Git服务器的SSH连接。同时,还概述了Git的基本概念、安装步骤、初始配置、常见命令及如何配置多个SSH-Key,适用于初学者快速上手Git操作。
683 51
Git学习笔记(一):基础与应用
|
设计模式 Java Go
【再谈设计模式】状态模式~对象行为的状态驱动者
状态模式属于行为型设计模式。它将对象的行为封装在不同的状态类中,使得对象在不同的状态下表现出不同的行为。上下文(Context):这是一个包含状态对象的类,它定义了客户感兴趣的接口,并维护一个具体状态对象的引用。上下文将操作委托给当前的状态对象来处理。抽象状态(State):这是一个抽象类或者接口,它定义了一个特定状态下的行为接口。所有具体的状态类都实现这个接口。具体状态(Concrete State):这些是实现抽象状态接口的类,每个具体状态类实现了与该状态相关的行为。
875 18
|
分布式计算 并行计算 数据处理
在 Django 上实现定时任务的指南
在 Django 中实现定时任务有多种方法,包括使用 Celery 和 Celery Beat、django-background-tasks、以及 APScheduler。根据您的需求和应用场景,可以选择最适合的方案。每种方法都有其优缺点,选择时应考虑任务复杂性、系统资源、以及维护成本。通过这些工具,您可以有效地管理和调度后台任务,提高应用程序的自动化水平和运行效率。
|
NoSQL API 调度
【Python】轻量级分布式任务调度系统-RQ
一 前言       Redis Queue 一款轻量级的P分布式异步任务队列,基于Redis作为broker,将任务存到redis里面,然后在后台执行指定的Job。就目前而言有三套成熟的工具celery,huey ,rq 。
5559 0