设为首页 收藏本站
查看: 979|回复: 0

Windows Phone 7 MVVM模式数据绑定和传递参数

[复制链接]

尚未签到

发表于 2015-5-8 11:23:02 | 显示全部楼层 |阅读模式
  数据绑定使用了ObservableCollection 类来实现,ViewModel通过继承GalaSoft.MvvmLight.ViewModelBase类来实现,Command
  使用GalaSoft.MvvmLight.Command.RelayCommand来实现。
  ObservableCollection表示一个动态数据集合,在添加项、移除项或刷新整个列表时,此集合将提供通知。
  客户列表绑定客户的名字、QQ、地址信息,单击的时候显示客户的全部详细信息。
DSC0000.jpg DSC0001.jpg
  View层





   
   
      
         
         
      
      
         
         
      
      
         
               
            
               
                  
                     
                        
                        
                     
                     
                  
               
            
               
            
                 
               
                        
                        
                 
            
         
      
   

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




namespace MvvmLight5.ViewModel
{
   public class ViewModelLocator
   {
      private static MainViewModel _main;
      ///
      /// 初始化  在这里创建ViewModel  可以将多个ViewModel在这里一起创建
      ///
      public ViewModelLocator()
      {
         CreateMain();
      }
      ///
      /// 获取MainViewModel的静态的实例对象
      ///
      public static MainViewModel MainStatic
      {
          get
          {
              if (_main == null)
              {
                  CreateMain();
              }
              return _main;
          }
      }
      ///
      /// 获取MainViewModel的实例对象
      ///
      public MainViewModel Main
      {
          get
          {
              return MainStatic;
          }
      }
      ///
      ///清理MainViewModel 退出程序的时候进行清理  在App.xmal.cs中调用
      ///
      public static void ClearMain()
      {
         _main.Cleanup();
         _main = null;
      }
      ///
      /// 创建MainViewModel
      ///
      public static void CreateMain()
      {
         if ( _main == null )
         {
            _main = new MainViewModel();
         }
      }
   }
}
  MainViewModel.cs




using System.Collections.ObjectModel;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using MvvmLight5.Model;
namespace MvvmLight5.ViewModel
{
   public class MainViewModel : ViewModelBase
   {
      ///
      /// 数据绑定的客户列表
      ///
      public ObservableCollection Customers
      {
         get
         {
            var customerCollection = new CustomerCollection();
            return customerCollection.Customers;
         }
      }
      //定义Command
      public RelayCommand DetailsPageCommand
      {
         get;
         private set;
      }
      public string ApplicationTitle
      {
         get
         {
            return "MVVM LIGHT";
         }
      }
      public string PageName
      {
         get
         {
            return "客户列表如下:";
         }
      }
      public string Welcome
      {
         get
         {
            return "Welcome to MVVM Light";
         }
      }
      ///
      /// 初始化 MainViewModel
      ///
      public MainViewModel()
      {
          //初始化Command
         DetailsPageCommand = new RelayCommand( ( msg ) => GoToDetailsPage( msg ) );
      }
      private object GoToDetailsPage( Customer msg )
      {
          System.Windows.MessageBox.Show("客户的详细资料如下    名字:" + msg.Name + "  城市:" + msg.City + "  地址:" + msg.Address + "  电话:" + msg.Phone + "  QQ:" + msg.QQ);
         return null;
      }
   }
}
  Model层
  Customers.cs




using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
namespace MvvmLight5.Model
{
   public class CustomerCollection
   {
      //在这里绑定数据使用了ObservableCollection 类  
      private readonly ObservableCollection _customers = new ObservableCollection();
      public ObservableCollection Customers
      {
         get { return _customers; }
      }
      public Customer GetCustomerByID( int id )
      {
         return _customers[ id ];
      }
      public CustomerCollection()
      {
         try
         {
            GenerateCustomers();
         }
         catch ( Exception e )
         {
            System.Windows.MessageBox.Show( "Exception: " + e.Message );
         }
      }
       //初始化数据
      public void GenerateCustomers()
      {
            _customers.Add( new Customer(1,"黄小琥","台湾高雄市十六街8号","高雄","13457789907","3232","huangxiaohu@qq.com") );
            _customers.Add(new Customer(2, "李开复", "北京市东城区十六街8号", "北京", "136589907", "787222894", "huasdsdu@qq.com"));
            _customers.Add(new Customer(3, "周杰伦", "台湾台北市十六街8号", "台北", "145455779907", "2323266", "232@qq.com"));
            _customers.Add(new Customer(4, "郎咸平", "香港十六街8号", "香港", "145489907", "787222894", "6ggg@qq.com"));
            _customers.Add(new Customer(5, "加菲猫", "高雄市十六街8号", "高雄市", "15777789907", "333434", "2323@qq.com"));
            _customers.Add(new Customer(6, "灰太狼", "台湾第三代市十六街8号", "高雄市", "134357789907", "23232", "3232@qq.com"));
            _customers.Add(new Customer(7, "喜洋洋", "台湾高雄市十六街8号", "高雄市", "134544589907", "23232777", "88sds@qq.com"));
            _customers.Add(new Customer(8, "春哥", "台湾所得税十六街8号", "高雄市", "13453445907", "888888", "sdsgg@qq.com"));
      }
   }
   public class Customer
   {
      public int ID { get; set; }
      public string Name { get ; set; }
      public string Address { get; set; }
      public string City { get; set; }
      public string Phone { get; set; }
      public string QQ { get; set; }
      public string Email { get; set; }
      public Customer()
      { }
      public Customer(
         int id,
          string name,
          string address,
          string city,
          string phone,
          string qq,
          string email )
      {
         ID = id;
         Name = name;
         Address = address;
         City = city;
         Phone = Phone;
         QQ = qq;
         Email = email;
      }
   }
}
  App.xaml 程序初始化处理





   
   
        
   
   
        
        
   

  cs




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

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-64943-1-1.html 上篇帖子: 70-599 微软Windows Phone 7开发人员证书考试真题 下篇帖子: 个性化你的Windows 7 Taskbar Thumbnail
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表