|
数据绑定使用了ObservableCollection 类来实现,ViewModel通过继承GalaSoft.MvvmLight.ViewModelBase类来实现,Command
使用GalaSoft.MvvmLight.Command.RelayCommand来实现。
ObservableCollection表示一个动态数据集合,在添加项、移除项或刷新整个列表时,此集合将提供通知。
客户列表绑定客户的名字、QQ、地址信息,单击的时候显示客户的全部详细信息。
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();
} |
|