Next, go to the previous demo. Next, set a refresh button to update the friend list. Here, you need to bind a command to the button. Of course, this command is a mainviewmodel attribute that implements the icommand interface;
1. Add the refreshcommand class to viewmodel.
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 refreshcommand: icommand {private readonly mainviewmodel _ viewmodel; Public refreshcommand (mainviewmodel viewmodel) {_ viewmodel = viewmodel;} public bool canexecute (object parameter) {return true;} public event eventhandler canexecutechanged; Public void execute (object parameter) {_ viewmodel. refresh ();}}}
2. Add the refresh command attribute in modelviewmodel.
Public icommand refreshcommand {Get; private set ;}
3. Add refresh data processing in modelviewmodel
Public void refresh () {observablecollection <friend> friends = new observablecollection <friend> (friendservice. getfriendsset (); this. Friends = friends; // update the friend list}
4. Add the refresh button on the page and add event binding
<Button canvas. Left = "24" canvas. Top = "556" command = "{binding refreshcommand}" content = "refersh" Height = "72"
Name = "btnrefresh" width = "160"/>
After running, click Refresh. The data list is updated.
Download source code