数据绑定使用了ObservableCollection<T> 类来实现,ViewModel通过继承GalaSoft.MvvmLight.ViewModelBase类来实现,Command

使用GalaSoft.MvvmLight.Command.RelayCommand<T>来实现。

ObservableCollection<T>表示一个动态数据集合,在添加项、移除项或刷新整个列表时,此集合将提供通知。

客户列表绑定客户的名字、QQ、地址信息,单击的时候显示客户的全部详细信息。

View层

  1. <phone:PhoneApplicationPage
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
  5. xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
  6. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  7. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  8. xmlns:Custom="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" xmlns:GalaSoft_MvvmLight_Command="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WP7"
  9. x:Class="MvvmLight5.MainPage"
  10. FontFamily="{StaticResource PhoneFontFamilyNormal}"
  11. FontSize="{StaticResource PhoneFontSizeNormal}"
  12. Foreground="{StaticResource PhoneForegroundBrush}"
  13. SupportedOrientations="Portrait"
  14. Orientation="Portrait"
  15. mc:Ignorable="d"
  16. d:DesignWidth="480"
  17. d:DesignHeight="768"
  18. shell:SystemTray.IsVisible="True"
  19. DataContext="{Binding Main, Source={StaticResource Locator}}">
  20. <!--绑定ViewModel中的Main实例对象 资源在App.xaml中进行了加载-->
  21. <Grid
  22. x:Name="LayoutRoot"
  23. Background="Transparent">
  24. <Grid.RowDefinitions>
  25. <RowDefinition  Height="Auto" />
  26. <RowDefinition Height="*" />
  27. </Grid.RowDefinitions>
  28. <StackPanel x:Name="TitlePanel"  Grid.Row="0" Margin="24,24,0,12">
  29. <TextBlock
  30. x:Name="ApplicationTitle"
  31. Text="{Binding ApplicationTitle}"
  32. Style="{StaticResource PhoneTextNormalStyle}" />
  33. <TextBlock
  34. x:Name="PageTitle"
  35. Text="{Binding PageName}"
  36. Margin="-3,-8,0,0"
  37. Style="{StaticResource PhoneTextTitle1Style}" />
  38. </StackPanel>
  39. <Grid  x:Name="ContentGrid" Grid.Row="1">
  40. <ListBox  x:Name="PersonListBox"   Margin="10" ItemsSource="{Binding Customers}">
  41. <!--绑定MainViewModel的Customers数据-->
  42. <ListBox.ItemTemplate>
  43. <DataTemplate>
  44. <StackPanel>
  45. <StackPanel
  46. x:Name="DataTemplateStackPanel"
  47. Orientation="Horizontal">
  48. <TextBlock
  49. x:Name="Name"
  50. Text="{Binding Name}"
  51. Margin="0,0,5,0"
  52. Style="{StaticResource PhoneTextExtraLargeStyle}" />
  53. <TextBlock
  54. x:Name="QQ"
  55. Text="{Binding QQ}"
  56. Margin="0"
  57. Style="{StaticResource PhoneTextExtraLargeStyle}" />
  58. </StackPanel>
  59. <TextBlock
  60. x:Name="Email"
  61. Text="{Binding Address}"
  62. Margin="0"
  63. Style="{StaticResource PhoneTextSubtleStyle}" />
  64. </StackPanel>
  65. </DataTemplate>
  66. </ListBox.ItemTemplate>
  67. <!--添加System.Windows.Interactivity触发器处理事件,放在ListBox里面-->
  68. <Custom:Interaction.Triggers>
  69. <!--当选中客户的时候触发该事件-->
  70. <Custom:EventTrigger EventName="SelectionChanged">
  71. <GalaSoft_MvvmLight_Command:EventToCommand x:Name="SelectionCommand" Command="{Binding DetailsPageCommand, Mode=OneWay}" CommandParameter="{Binding SelectedItem, ElementName=PersonListBox}"/>
  72. <!--传递的参数为ListBox选中的Customer对象-->
  73. </Custom:EventTrigger>
  74. </Custom:Interaction.Triggers>
  75. </ListBox>
  76. </Grid>
  77. </Grid>
  78. </phone:PhoneApplicationPage>

ViewModel层

ViewModelLocator是对ViewModel进行初始化和清理的集中处理的类 添加资源的时候只需要添加这一个类就可以了。

ViewModelLocator.cs

  1. namespace MvvmLight5.ViewModel
  2. {
  3. public class ViewModelLocator
  4. {
  5. private static MainViewModel _main;
  6. /// <summary>
  7. /// 初始化  在这里创建ViewModel  可以将多个ViewModel在这里一起创建
  8. /// </summary>
  9. public ViewModelLocator()
  10. {
  11. CreateMain();
  12. }
  13. /// <summary>
  14. /// 获取MainViewModel的静态的实例对象
  15. /// </summary>
  16. public static MainViewModel MainStatic
  17. {
  18. get
  19. {
  20. if (_main == null)
  21. {
  22. CreateMain();
  23. }
  24. return _main;
  25. }
  26. }
  27. /// <summary>
  28. /// 获取MainViewModel的实例对象
  29. /// </summary>
  30. public MainViewModel Main
  31. {
  32. get
  33. {
  34. return MainStatic;
  35. }
  36. }
  37. /// <summary>
  38. ///清理MainViewModel 退出程序的时候进行清理  在App.xmal.cs中调用
  39. /// </summary>
  40. public static void ClearMain()
  41. {
  42. _main.Cleanup();
  43. _main = null;
  44. }
  45. /// <summary>
  46. /// 创建MainViewModel
  47. /// </summary>
  48. public static void CreateMain()
  49. {
  50. if ( _main == null )
  51. {
  52. _main = new MainViewModel();
  53. }
  54. }
  55. }
  56. }

MainViewModel.cs

  1. using System.Collections.ObjectModel;
  2. using GalaSoft.MvvmLight;
  3. using GalaSoft.MvvmLight.Command;
  4. using MvvmLight5.Model;
  5. namespace MvvmLight5.ViewModel
  6. {
  7. public class MainViewModel : ViewModelBase
  8. {
  9. /// <summary>
  10. /// 数据绑定的客户列表
  11. /// </summary>
  12. public ObservableCollection<Customer> Customers
  13. {
  14. get
  15. {
  16. var customerCollection = new CustomerCollection();
  17. return customerCollection.Customers;
  18. }
  19. }
  20. //定义Command
  21. public RelayCommand<Customer> DetailsPageCommand
  22. {
  23. get;
  24. private set;
  25. }
  26. public string ApplicationTitle
  27. {
  28. get
  29. {
  30. return "MVVM LIGHT";
  31. }
  32. }
  33. public string PageName
  34. {
  35. get
  36. {
  37. return "客户列表如下:";
  38. }
  39. }
  40. public string Welcome
  41. {
  42. get
  43. {
  44. return "Welcome to MVVM Light";
  45. }
  46. }
  47. /// <summary>
  48. /// 初始化 MainViewModel
  49. /// </summary>
  50. public MainViewModel()
  51. {
  52. //初始化Command
  53. DetailsPageCommand = new RelayCommand<Customer>( ( msg ) => GoToDetailsPage( msg ) );
  54. }
  55. private object GoToDetailsPage( Customer msg )
  56. {
  57. System.Windows.MessageBox.Show("客户的详细资料如下    名字:" + msg.Name + "  城市:" + msg.City + "  地址:" + msg.Address + "  电话:" + msg.Phone + "  QQ:" + msg.QQ);
  58. return null;
  59. }
  60. }
  61. }

Model层

Customers.cs

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. namespace MvvmLight5.Model
  5. {
  6. public class CustomerCollection
  7. {
  8. //在这里绑定数据使用了ObservableCollection<T> 类
  9. private readonly ObservableCollection<Customer> _customers = new ObservableCollection<Customer>();
  10. public ObservableCollection<Customer> Customers
  11. {
  12. get { return _customers; }
  13. }
  14. public Customer GetCustomerByID( int id )
  15. {
  16. return _customers[ id ];
  17. }
  18. public CustomerCollection()
  19. {
  20. try
  21. {
  22. GenerateCustomers();
  23. }
  24. catch ( Exception e )
  25. {
  26. System.Windows.MessageBox.Show( "Exception: " + e.Message );
  27. }
  28. }
  29. //初始化数据
  30. public void GenerateCustomers()
  31. {
  32. _customers.Add( new Customer(1,"黄小琥","台湾高雄市十六街8号","高雄","13457789907","3232","huangxiaohu@qq.com") );
  33. _customers.Add(new Customer(2, "李开复", "北京市东城区十六街8号", "北京", "136589907", "787222894", "huasdsdu@qq.com"));
  34. _customers.Add(new Customer(3, "周杰伦", "台湾台北市十六街8号", "台北", "145455779907", "2323266", "232@qq.com"));
  35. _customers.Add(new Customer(4, "郎咸平", "香港十六街8号", "香港", "145489907", "787222894", "6ggg@qq.com"));
  36. _customers.Add(new Customer(5, "加菲猫", "高雄市十六街8号", "高雄市", "15777789907", "333434", "2323@qq.com"));
  37. _customers.Add(new Customer(6, "灰太狼", "台湾第三代市十六街8号", "高雄市", "134357789907", "23232", "3232@qq.com"));
  38. _customers.Add(new Customer(7, "喜洋洋", "台湾高雄市十六街8号", "高雄市", "134544589907", "23232777", "88sds@qq.com"));
  39. _customers.Add(new Customer(8, "春哥", "台湾所得税十六街8号", "高雄市", "13453445907", "888888", "sdsgg@qq.com"));
  40. }
  41. }
  42. public class Customer
  43. {
  44. public int ID { get; set; }
  45. public string Name { get ; set; }
  46. public string Address { get; set; }
  47. public string City { get; set; }
  48. public string Phone { get; set; }
  49. public string QQ { get; set; }
  50. public string Email { get; set; }
  51. public Customer()
  52. { }
  53. public Customer(
  54. int id,
  55. string name,
  56. string address,
  57. string city,
  58. string phone,
  59. string qq,
  60. string email )
  61. {
  62. ID = id;
  63. Name = name;
  64. Address = address;
  65. City = city;
  66. PhonePhone = Phone;
  67. QQ = qq;
  68. Email = email;
  69. }
  70. }
  71. }

App.xaml 程序初始化处理

  1. <Application x:Class="MvvmLight5.App"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
  5. xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
  6. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  7. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  8. mc:Ignorable="d"
  9. xmlns:vm="clr-namespace:MvvmLight5.ViewModel">
  10. <!--Application Resources-->
  11. <Application.Resources>
  12. <vm:ViewModelLocator x:Key="Locator"
  13. d:IsDataSource="True" />
  14. </Application.Resources>
  15. <Application.ApplicationLifetimeObjects>
  16. <!--Required object that handles lifetime events for the application-->
  17. <shell:PhoneApplicationService Launching="Application_Launching"
  18. Closing="Application_Closing"
  19. Activated="Application_Activated"
  20. Deactivated="Application_Deactivated" />
  21. </Application.ApplicationLifetimeObjects>
  22. </Application>

cs

// 清理ViewModel资源
      private void Application_Closing( object sender, ClosingEventArgs e )
      {
         ViewModelLocator.ClearMain();
      }

转载于:https://blog.51cto.com/linzheng/1078630

Windows Phone 7 MVVM模式数据绑定和传递参数相关推荐

  1. Windows Phone 7 MVVM模式通讯方式之实现Command

    MVVM模式的View与ViewModel的三大通讯方式:Binding Data(实现数据的传递).Command(实现操作的调用)和Attached Behavior(实现控件加载过程中的操作). ...

  2. wpf mvvm模式下CommandParameter传递多参

    wpf mvvm模式下CommandParameter传递多参 原文:wpf mvvm模式下CommandParameter传递多参 CommandParameter一般只允许设置一次,所以如果要传递 ...

  3. 通用Windows应用《博客园-开发者的网上家园》开发(1)——MVVM模式

    最近开发了个WP8.1和Windows8.1平台上的应用--<博客园-开发者的网上家园>,基于 Windows Runtime .在此有必要说明一下,WP8.0以前的应用程序是基于Silv ...

  4. C# 浅谈基于Wpf下的MVVM模式的设计思想

    目录 一.Model实体层 二.ViewModel视图模型层 1.定义属性通知基类 1.1 数据验证接口的实现 1.2 验证标识类定义 2.ViewModel前端交互实现 2.1 ICommand命令 ...

  5. Android MVC,MVP,MVVM模式入门——重构登陆注册功能

    一  MVC模式: M:model,业务逻辑 V:view,对应布局文件 C:Controllor,对应Activity 项目框架: 代码部分: layout文件(适用于MVC和MVP两个Demo): ...

  6. WPF MVVM从入门到精通1:MVVM模式简介

    WPF MVVM从入门到精通1:MVVM模式简介 原文:WPF MVVM从入门到精通1:MVVM模式简介 WPF MVVM从入门到精通1:MVVM模式简介 WPF MVVM从入门到精通2:实现一个登录 ...

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

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

  8. [Silverlight]使用MVVM模式打造英汉词典

    最近比较关注MVVM(Model-View_ViewModel)模式,该模式十分适合WPF/Silverlight的开发.出于练习的目的打算使用Silverlight做个英汉词典(可能是由于近来疯狂的 ...

  9. mvvm模式和mvc的区别_被误解的 MVC 和被神化的 MVVM,值得收藏!

    MVC 的历史 MVC,全称是 Model View Controller,是模型 (model)-视图 (view)-控制器 (controller) 的缩写.它表示的是一种常见的客户端软件开发框架 ...

最新文章

  1. NYOJ 90 —— 求正整数n划分为若干个正整数的划分个数
  2. Android UI的优化
  3. 剖析供应链攻击的防范
  4. Scrapy周期性爬取(解决Unknown command: crawl报错)
  5. 对话指令集创始人兼CEO潘爱民:面向未来的新型物联网操作系统 | 人物志
  6. 拷贝构造函数和赋值构造函数声明为私有的作用
  7. JS - javascript容错处理代码
  8. 登录双token方案
  9. 分子动力学软件-VMD(win版)
  10. 《蜥蜴脑法则》读后感
  11. 集团公司预算控制与网上费用报销系统
  12. eclipse配置python使用相对路径_eclipse配置python环境详解
  13. CHINA TOP国家杯:用电子竞技搭建中国文化走出去的平台
  14. 真心话大冒险,一款小程序让你看清你朋友的内心
  15. “ Linux基础知识学习 ” 之 关于rc.d文件的理解 04
  16. VirtualBox虚拟机网络设置(四种方式)
  17. JavaSE - 数组的相关算法
  18. 完整elasticsearch安装及其插件安装
  19. C#获取本周,上周,下周信息
  20. 爬虫利器:Python获取免费IP代理

热门文章

  1. cheerio api
  2. django【orm操作】
  3. 303. Range Sum Query - Immutable
  4. “赋值”与“初始化”
  5. LeetCode 52. N-Queens II
  6. 把aspx页面输出成xml的方法注意事项
  7. 组态王6.55连接MySql数据库(笔记)
  8. CentOS7 DNS的添加
  9. Vb Shell 打开程序 等待运行完毕后再继续
  10. gym 101858