Introduced
Re-imagine the bindings of Windows 8 Store Apps
Add, delete, modify and query data through MVVM mode
Example
1, Model layer
Binding/mvvm/model/productdatabase.cs
* * * Model layer Data persistence operation (local or remote) * * This example is only a demo/using System;
Using System.Collections.Generic;
Using System.Linq; Namespace XamlDemo.Binding.MVVM.Model {public class Productdatabase {private list<product> _produc
ts = null; Public list<product> getproducts () {if (_products = null) {Random
Random = new Random ();
_products = new list<product> (); for (int i = 0; i < i++) {_products.
ADD (new Product {ProductId = i, Name = "Name" + i.tostring (). PadLeft (4, ' 0 '), Category = "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 layer of the entity class, if need to notify the need to implement INotifyPropertyChanged interface * * 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 ("ProductId");
}} private string _name;
public string Name {get {_name;}
set {_name = value;
raisePropertyChanged ("Name");
}} private string _category;
public string Category {get {return _category;}
set {_category = value; raisePropertyChanged ("Category");
} public event PropertyChangedEventHandler propertychanged;
protected void raisePropertyChanged (string name) {if (propertychanged!= null) {
PropertyChanged (This, new PropertyChangedEventArgs (name)); }
}
}
}