Windows phone7-mvvm advanced

Source: Internet
Author: User

Description

In mvvm, We can bind an object implementing icommand to the corresponding elment to interact with the viewmodel. mvvm will update the data source bound to the page. Today we will add a friend.

It is easy to get user input and add the input object to the Collection ..

1. Add the attribute friend to viewmodel.

 
Private friend _ friend; // <summary> // receives the input constructed friend /// </Summary> Public friend {get {return _ friend ;} set {If (value = _ friend) {return;} _ Friend = value; raisepropertychanged ("friend ");}}

2. Add the accept input control on the page.

3. Add the bind and save button

 
   
   
   
   
   
   
<Button canvas. left = "167" canvas. top = "163" content = "save" Height = "69" X: Name = "save" width = "123" command = "{binding addcommand}"/>

4. Add and save the corresponding addcommand and add addcommand in viewmodel (You know)

Using system; using system. net; using system. windows; using system. windows. controls; using system. windows. documents; using system. windows. ink; using system. windows. input; using system. windows. media; using system. windows. media. animation; using system. windows. shapes; namespace hellowp7.viewmodel {public class addcommand: icommand {private readonly mainviewmodel _ viewmodel; Public addcommand (mainviewmodel viewmodel) {_ viewmodel = viewmodel;} public bool canexecute (object parameter) {return true;} public event eventhandler canexecutechanged; Public void execute (object parameter) {_ viewmodel. add (_ viewmodel. friend );}}}

 

Add attributes and add methods in viewmode:

Public icommand addcommand {Get; private set ;}
 
/// <Summary> /// save /// </Summary> /// <Param name = "friend"> </param> Public void add (friend) {friendservice. addfriend (friend); // Add to the set this. friends = new observablecollection <friend> (friendservice. getfriendsset (); this. friend = new friend (); // clear}

And initialize addcommand in the viewmode constructor.

Public mainviewmodel () {// initialize data _ Title = "friends book"; _ friends = new observablecollection <friend> (friendservice. getfriends (); refreshcommand = new refreshcommand (this); // refresh event addcommand = new addcommand (this );}

In friendservice, it is very easy to add friend to the set with the friend name as the primary key

Using system; using system. collections. generic; using system. LINQ; namespace hellowp7.model {public class friendservice {public list <friend> getfriends () {list <friend> friends = new list <friend> () {new friend () {name = "Johnny", address = "Beijing", phonenum = "13621010899"}, new friend () {name = "David", address = "Shanghai ", phonenum = "23621010899"}, new friend () {name = "John", address = "Tianjin", phonenum = "33621010899"}, new friend () {name = "Susan", address = "Wuhan", phonenum = "43621010899"}, new friend () {name = "Badboy", address = "Shenzhen ", phonenum = "53621010899"}, new friend () {name = "Jobs", address = "Guangzhou", phonenum = "11621010899"}, new friend () {name = "Kevin", address = "Shenzhen", phonenum = "53621010899" }}; return friends ;} /// <summary> // use static variables as data containers // </Summary> Private Static list <friend> _ friends = new list <friend> () {new friend () {name = "Jay", address = "Taipei", phonenum = "8888888888" }}; public list <friend> getfriendsset () {return _ friends ;} public void addfriend (friend) {bool ishave = false; int Index = 0; foreach (VAR item in _ friends) {If (item. name = friend. name) {ishave = true; break;} index ++;} If (ishave) {_ friends [Index] = friend;} else {_ friends. add (friend );}}}}
An exception occurred during running. No reason for initializing friend in viewmodel was found;
 
Now, you can automatically add and update the list. effect:

 

Download source code

Related Article

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.