第一个MVVM wp7程序
创建一个MVVM wp7程序,从手动组建MVVM程序到使用MVVM Light Toolkit快速创建MVVM程序
一、一步步走向MVVM(一个简单的好友列表)
打开vs2010 Express for windows phone,创建一个Windows Phone Application
这是开始的项目结构
创建连个文件夹Model和ViewModel,并在ViewModel中添加类ViewModel,实现INotifyPropertyChanged接口,并对PropertyChanged进行一点封装
MainViewModel.cs
MainViewModel
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;using HelloWp7.Model;
namespace HelloWp7.ViewModel
{public class MainViewModel : INotifyPropertyChanged{public MainViewModel(){//初始化数据_title = "Friends Book";_friends = new ObservableCollection<Friend>(friendService.GetFriends());}#region dataServicesprivate FriendService friendService=new FriendService();#endregion/// <summary>/// 应用名称/// </summary>private string _appName;public string AppName{get{return _appName;}set{if (_appName == value){return;}_appName = value;RaisePropertyChanged("AppName");}}private string _title;/// <summary>/// 标题/// </summary>public string Title{get{return _title;}set{if (_title == value){return;}_title = value;RaisePropertyChanged("Title");}}private ObservableCollection<Friend> _friends;public ObservableCollection<Friend> Friends{get{return _friends;}set{if (value == _friends){return;}_friends = value;RaisePropertyChanged("Friends");}}public event PropertyChangedEventHandler PropertyChanged;public void RaisePropertyChanged(string propertyName){if (PropertyChanged != null){PropertyChanged(this, new PropertyChangedEventArgs(propertyName));}}}
}
前台界面:
1.先添加对ViewModel的命名空间的引用
xmlns:mv="clr-namespace:HelloWp7.ViewModel"
2.将MainViewModel作为资源配置在页面上
<phone:PhoneApplicationPage.Resources><!--配置ViewModel为资源,执行时会直接初始化--><mv:MainViewModel x:Name="MainViewModel"></mv:MainViewModel></phone:PhoneApplicationPage.Resources>
3.将MainViewModel作为DataContext绑定到页面最顶层的Controll上
<Grid x:Name="LayoutRoot" Background="Transparent" DataContext="{StaticResource MainViewModel}">
4.到具体要显示的控件上绑定要显示的内容就行了
MainPage.xaml
<phone:PhoneApplicationPage x:Class="HelloWp7.MainPage"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:mv="clr-namespace:HelloWp7.ViewModel"mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"FontFamily="{StaticResource PhoneFontFamilyNormal}"FontSize="{StaticResource PhoneFontSizeNormal}"Foreground="{StaticResource PhoneForegroundBrush}"SupportedOrientations="Portrait" Orientation="Portrait"shell:SystemTray.IsVisible="True"><phone:PhoneApplicationPage.Resources><!--配置ViewModel为资源,执行时会直接初始化--><mv:MainViewModel x:Name="MainViewModel"></mv:MainViewModel><DataTemplate x:Name="FriendTemplate" ><StackPanel Width="400" Margin="0,5" VerticalAlignment="Top"><TextBlock FontSize="39" Text="{Binding Name}"></TextBlock><StackPanel Orientation="Horizontal"><TextBlock Text="{Binding Address}" VerticalAlignment="Top"></TextBlock> <TextBlock Text="{Binding PhoneNum}" Margin="10,0" VerticalAlignment="Top"></TextBlock></StackPanel></StackPanel></DataTemplate><Style x:Key="frdlist" TargetType="ListBox"><Setter Property="BorderBrush" Value="White"></Setter></Style></phone:PhoneApplicationPage.Resources><!--LayoutRoot is the root grid where all page content is placed--><Grid x:Name="LayoutRoot" Background="Transparent" DataContext="{StaticResource MainViewModel}"><Grid.RowDefinitions><RowDefinition Height="Auto"/><RowDefinition Height="*"/></Grid.RowDefinitions><!--TitlePanel contains the name of the application and page title--><StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28"><TextBlock x:Name="ApplicationTitle" Text="{Binding AppName}" Style="{StaticResource PhoneTextNormalStyle}"/><TextBlock x:Name="PageTitle" Text="{Binding Title}" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/></StackPanel><!--ContentPanel - place additional content here--><Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"></Grid> <Canvas Grid.Row="1"><ListBox ItemsSource="{Binding Friends}" ItemTemplate="{StaticResource FriendTemplate}" Width="468" Height="370" Canvas.Top="0" /> </Canvas></Grid><!--Sample code showing usage of ApplicationBar--><!--<phone:PhoneApplicationPage.ApplicationBar><shell:ApplicationBar IsVisible="True" IsMenuEnabled="True"><shell:ApplicationBarIconButton IconUri="/Images/appbar_button1.png" Text="Button 1"/><shell:ApplicationBarIconButton IconUri="/Images/appbar_button2.png" Text="Button 2"/><shell:ApplicationBar.MenuItems><shell:ApplicationBarMenuItem Text="MenuItem 1"/><shell:ApplicationBarMenuItem Text="MenuItem 2"/></shell:ApplicationBar.MenuItems></shell:ApplicationBar></phone:PhoneApplicationPage.ApplicationBar>--></phone:PhoneApplicationPage>
MainPage.xaml.cs中没有任何代码(MVVM中不会有事件处理了而是用Command绑定就可以了)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;namespace HelloWp7
{public partial class MainPage : PhoneApplicationPage{// Constructorpublic MainPage(){InitializeComponent();}}
}
另外两个Model类
Friend.cs
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;namespace HelloWp7.Model
{public class Friend{ public string Name { get; set; }public string Address { get; set; }public string Pic { get; set; }public string PhoneNum { get; set; }}
}
FriendService.cs
using System;
using System.Collections.Generic;namespace HelloWp7.Model
{public class FriendService{public List<Friend> GetFriends(){List<Friend> friends = new List<Friend>() { new Friend(){ Name="Johnny", Address="北京", PhoneNum="13621010899"}, new Friend(){ Name="David", Address="上海", PhoneNum="23621010899"}, new Friend(){ Name="John", Address="天津", PhoneNum="33621010899"}, new Friend(){ Name="Susan", Address="武汉", PhoneNum="43621010899"}, new Friend(){ Name="Badboy", Address="深圳", PhoneNum="53621010899"},new Friend(){ Name="Jobs", Address="广州", PhoneNum="11621010899"}, new Friend(){ Name="kevin", Address="深圳", PhoneNum="53621010899"}};return friends;}}
}
项目目前结构为:
按F5发布到模拟器(Windows Phone Emulator)中效果为
下载源码
转载于:https://www.cnblogs.com/yoainet/archive/2011/12/07/2279232.html
第一个MVVM wp7程序相关推荐
- 模拟 Vue 手写一个 MVVM
原文出自:https://www.pandashen.com MVVM 的前世今生 MVVM 设计模式,是由 MVC(最早来源于后端).MVP 等设计模式进化而来,M - 数据模型(Model),VM ...
- php实现mvvm,“Why Not”使用QML实现一个MVVM框架
"Why Not"使用QML实现一个MVVM框架 一.前言最近几年最热门的技术之一就是前端技术了,各种前端框架,前端标准和前端设计风格层出不穷,而在众多前端框架中具有MVC,MVV ...
- 【2019 前端进阶之路】深入 Vue 响应式原理,活捉一个 MVVM
作者:江三疯,专注前端开发.欢迎关注公众号前端发动机,第一时间获得作者文章推送,还有各类前端优质文章,致力于成为推动前端成长的引擎. 前言 作为 Vue 面试中的必考题之一,Vue 的响应式原理,想必 ...
- 构建一个移动应用程序要花多少钱?
构建一个移动应用程序要花多少钱? How much does it cost to build a mobile app? 不幸的是,对于一个移动应用程序的开发成本应该是多少这个问题,没有一个单一的答 ...
- 在纸上写好一个c语言程序后,上机运行的基本步骤为,c基本概念(选择题).docx
PAGE / NUMPAGES 一.单选题 1.下面叙述中正确的是: (A) 在C语言程序中,main()函数必须放在程序的开始位置 (B) 在C语言程序中,要调用的函数必须在main()函数中定义 ...
- 如何成为一个优秀的程序员
本文给出了十五个评定软件开发人员的标准,可以帮助程序员朋友从一个好的程序员成为一个优秀的程序员,和大家共飨! 怎样评定一名软件开发人员?这是一个颇为奇怪的问题.现在已经有了很多的理论和形式来做这件事, ...
- 一起学WPF系列(2):第一个WPF应用程序
概述 Windows Presentation Foundation (WPF) 是下一代显示系统,用于生成能带给用户震撼视觉体验的 Windows 客户端应用程序.使用 WPF,您可以创建广泛的独立 ...
- 分享下自己写的一个微信小程序请求远程数据加载到页面的代码
分享下自己写的一个微信小程序请求远程数据加载到页面的代码 1 思路整理 就是页面加载完毕的时候 请求远程接口,然后把数据赋值给页面的变量 ,然后列表循环 2 js相关代码 我是改的 onload ...
- 关于程序员的那些事——一个五年程序员的总结
一晃,做了五年多的程序员了,感觉自己应该是程序员大军中比较典型的一个,班科生毕业,直接加入了软件开发的行列,先是小公司,然后大公司,走了一条典型的程序员的路线. 虽然我不是大牛,但是感觉在我这样普通程 ...
最新文章
- c语言位段sizeof,C语言位段的介绍
- html盒子移动动画代码,HTML5/Canvas 盒子追踪动画
- 阿里云分布式容器平台即将全面启动公测
- 对于如何删除redis中geo存入的坐标
- 嵌入式与单片机之间的关系是什么?
- 由于TempDB设置错误导致SQL Server无法重启错误的解决方案
- sql,dateadd,datediff
- 2015.4.19 为什么footer下a的索引值那么大
- 远程桌面 Web 连接
- 工业相机选型_工业相机与镜头的选型方法
- 样条曲面_SolidWorks肥皂块曲面建模,你的肥皂掉了
- Python 之 文件
- 字典(JSON)与模型的转换
- FFmpeg 视频裁剪
- 给一个年份输出该年是否举办足球世界杯的信息,以及给一个国家的名称输出是否夺得过世界杯冠军
- 关于用ANSYS有限元仿真软件划分网格的一些体验
- PAT甲级英文单词整理
- 计算机为什么老是重启,电脑总是已计划自动重启怎么办 已计划自动重启怎么关闭...
- 2021物理化学实验8:乙酸乙酯皂化反应动力学研究
- 【2022年度总结】总结过去,展望未来
热门文章
- 在html中怎么写加起来的,css可以在html里面写吗?
- HTML+CSS+JS实现 ❤️3D洞穴无限延伸动画特效❤️
- 《SpringCloud超级入门》Spring Boot Starter的介绍及使用《七》
- 面试高频题:springboot自动装配的原理你能说出来吗?
- 打开python环境_windows下切换Python运行环境。
- android 播放器 反交错,反交错- 视频横纹处理
- java 函数签名,Java签名getAlgorithm()方法
- 计算机语言wifi,Windows7WIFI热点设置器
- 用程序同步mysql数据库表_初次用Java写了个数据库表同步工具
- Java 输出链表的第一个和最后一个元素