Windows 8 Store Apps Learning (55) Binding: MVVM mode

Source: Internet
Author: User

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)); }
        }
    }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.