鸿 网 互 联 www.68idc.cn

当前位置 : 服务器租用 > .net技术 > WinRT/Metro > >

重新想象 Windows 8 Store Apps (55)

来源:互联网 作者:佚名 时间:2013-11-13 10:12
重新想象 Windows 8 Store Apps (55) - 绑定: MVVM 模式Posted on [源码下载] 重新想象 Windows 8 Store Apps (55) - 绑定: MVVM 模式 作者:webabcd 介绍 重新想象 Windows 8 Store Apps 之绑定 通过 MVVM 模式实现数据的添加、删除、修改和查询 示例 1、Mo

重新想象 Windows 8 Store Apps (55) - 绑定: MVVM 模式 Posted on

[源码下载]


重新想象 Windows 8 Store Apps (55) - 绑定: MVVM 模式



作者:webabcd


介绍
重新想象 Windows 8 Store Apps 之 绑定

  • 通过 MVVM 模式实现数据的添加、删除、修改和查询


  • 示例
    1、Model 层
    Binding/MVVM/Model/ProductDatabase.cs

    /* * Model 层的数据持久化操作(本地或远程) * * 本例只是一个演示 */ using System; using System.Collections.Generic; using System.Linq; namespace XamlDemo.Binding.MVVM.Model { public class ProductDatabase { private List<Product> _products = null; public List<Product> GetProducts() { if (_products == null) { Random random = new Random(); _products = new List<Product>(); for (int i = 0; i < 100; i++) { _products.Add( new Product { ProductId = i, Name = + i.ToString().PadLeft(), Category = + (char)random.Next(65, 91) }); } } return _products; } public List<Product> GetProducts(string name, string category) { return GetProducts().Where(p => p.Name.Contains(name) && p.Category.Contains(category)).ToList(); } public void Update(Product product) { var oldProduct = _products.Single(p => p.ProductId == product.ProductId); oldProduct = product; } public Product Add(string name, string category) { Product product =new Product(); product.ProductId = _products.Max(p => p.ProductId) + 1; product.Name = name; product.Category = category; _products.Insert(0, product); return product; } public void Delete(Product product) { _products.Remove(product); } } }

    Binding/MVVM/Model/Product.cs

    /* * Model 层的实体类,如果需要通知则需要实现 INotifyPropertyChanged 接口 */ using System.ComponentModel; namespace XamlDemo.Binding.MVVM.Model { public class Product : INotifyPropertyChanged { public Product() { ProductId = 0; Name = ""; Category = ""; } private int _productId; public int ProductId { get { return _productId; } set { _productId = value; RaisePropertyChanged(); } } private string _name; public string Name { get { return _name; } set { _name = value; RaisePropertyChanged(); } } private string _category; public string Category { get { return _category; } set { _category = value; RaisePropertyChanged(); } } public event PropertyChangedEventHandler PropertyChanged; protected void RaisePropertyChanged(string name) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(name)); } } } }


    2、ViewModel 层
    Binding/MVVM/ViewModel/ProductViewModel.cs

    /* * ViewModel 层 */ using System.Collections.ObjectModel; using System.Windows.Input; using XamlDemo.Binding.MVVM.Model; using System.Linq; using System.ComponentModel; namespace XamlDemo.Binding.MVVM.ViewModel { public class ProductViewModel { ObservableCollection<Product> Products { get; set; } Product Product { get; set; } private ProductDatabase _context = null; public ProductViewModel() { _context = new ProductDatabase(); Product = new Product(); Products = new ObservableCollection<Product>(); } ICommand GetProductsCommand { get { return new GetProductsCommand(this); } } public void GetProducts(Product query) { products = _context.GetProducts(query.Name, query.Category); // 更新 ViewModel 中的数据 Products.Clear(); foreach (var product in products) { Products.Add(product); } } ICommand AddProductCommand { get { return new AddProductCommand(this); } } public void AddProduct(Product product) { newProduct = _context.Add(product.Name, product.Category); // 更新 ViewModel Products.Insert(0, newProduct); } ICommand UpdateProductCommand { get { return new UpdateProductCommand(this); } } public void UpdateProduct(Product product) { // 更新 ViewModel product.Name = product.Name + ; product.Category = product.Category + ; // 更新 Model _context.Update(product); } ICommand DeleteProductCommand { get { return new DeleteProductCommand(this); } } public void DeleteProduct(Product product) { // 更新 Model _context.Delete(product); // 更新 ViewModel Products.Remove(product); } } }

    Binding/MVVM/ViewModel/AddProductCommand.cs

    网友评论
    <