Command for communication in mvvm mode in Windows Phone 7

Source: Internet
Author: User
Three communication modes of view and viewmodel in mvvm mode: Binding data (data transmission), command (call to implement operations), and attached behavior (operations during control loading ). (1) Binding data for communication in mvvm mode of Windows Phone 7. (2) command for communication in mvvm mode of Windows Phone 7. (3) attached behavior for communication in mvvm mode of Windows Phone 7. The following uses an instance to implement command communication in mvvm mode (1) for mainpage. XAML files Code To implement the view layer.
<Phone: phoneapplicationpage X: class = "commanddemo. mainpage "xmlns =" http://schemas.microsoft.com/winfx/2006/xaml/presentation "xmlns: x =" http://schemas.microsoft.com/winfx/2006/xaml "xmlns: Phone =" CLR-namespace: Microsoft. phone. controls; Assembly = Microsoft. phone "xmlns: shell =" CLR-namespace: Microsoft. phone. shell; Assembly = Microsoft. phone "xmlns: D =" http://schemas.microsoft.com/expression/blend/2008 "xmlns: m C = "http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns: My = "CLR-namespace: commanddemo. viewmodel "xmlns: my_interactivity =" CLR-namespace: commanddemo. command "xmlns: Custom =" CLR-namespace: system. windows. interactivity; Assembly = system. windows. interactivity "xmlns: Ic =" CLR-namespace: Microsoft. expression. interactivity. core; Assembly = Microsoft. expression. interactions "MC: ignorable =" D "D: designw Idth = "480" D: designheight = "768" fontfamily = "{staticresource quota}" fontsize = "{staticresource quota}" foreground = "{staticresource quota}" supportedorientations = "portrait" orientation = "portrait" Shell: systemtray. isvisible = "true"> <! -- Set the context data datacontext of the entire page to radiusviewmodel --> <Phone: phoneapplicationpage. datacontext> <my: radiusviewmodel/> </Phone: phoneapplicationpage. datacontext> <grid X: Name = "layoutroot" background = "Transparent"> <grid. rowdefinitions> <rowdefinition Height = "Auto"/> <rowdefinition Height = "*"/> </grid. rowdefinitions> <stackpanel X: Name = "titlepanel" grid. row = "0" margin = ","> <textblock X: name = "applicationtitle" text = "my application" style = "{staticresource phonetextnormalstyle}"/> <textblock X: Name = "pagetitle" text = "command" margin = "9, -7,0, 0 "style =" {staticresource phonetexttitle1style} "/> </stackpanel> <grid X: Name =" contentpanel "grid. row = "1" margin = "12, 0, "> <ellipse fill =" red "Height =" {binding radius} "width =" {binding radius} "horizontalalignment =" Left "margin =", 84, 0, 0 "name =" ellipse1 "stroke =" black "strokethickness =" 1 "verticalignment =" TOP "/> <button content =" small "Height =" 72 "horizontalalignment =" left "margin =" 0,385, 160 "name =" button1 "verticalignment =" TOP "width =" "> <custom: interaction. triggers> <custom: eventtrigger eventname = "click"> <my_interactivity: executecommandaction commandname = "minradius"/> </custom: eventtrigger> </custom: interaction. triggers> </button> <button content = "medium" Height = "72" horizontalalignment = "Left" margin = "149,384, 160 "name =" button2 "verticalignment =" TOP "width =" "> <custom: interaction. triggers> <custom: eventtrigger eventname = "click"> <my_interactivity: executecommandaction commandname = "medradius"/> </custom: eventtrigger> </custom: interaction. triggers> </button> <button content = "large" Height = "72" horizontalalignment = "Left" margin = "299,382, 160 "name =" button3 "verticalignment =" TOP "width =" "> <custom: interaction. triggers> <custom: eventtrigger eventname = "click"> <my_interactivity: executecommandaction commandname = "maxradius"/> </custom: eventtrigger> </custom: interaction. triggers> </button> </GRID> </Phone: phoneapplicationpage>
(2) Code of the radiusviewmodel. CS file to implement the viewmodel Layer
Using system; using system. windows. input; using system. componentmodel; using Microsoft. expression. interactivity. core; namespace commanddemo. viewmodel {public class radiusviewmodel: inotifypropertychanged {private double radius; Public radiusviewmodel () {radius = 0; minradius = new actioncommand (P => radius = 100 ); medradius = new actioncommand (P => radius = 200); maxradius = new actioncommand (P => r Adius = 300);} public event propertychangedeventhandler propertychanged; Public icommand minradius {Get; private set;} public icommand medradius {Get; private set;} public icommand maxradius {Get; private set;} public Double Radius {get {return radius;} set {radius = value; onpropertychanged ("radius") ;}} protected virtual void onpropertychanged (string propertyname) {var propertychang Ed = propertychanged; If (propertychanged! = NULL) propertychanged (this, new propertychangedeventargs (propertyname ));}}}
(3) executecommandaction. CS class to implement command operation
Using system; using system. windows; using system. windows. input; using system. windows. interactivity; using system. reflection; namespace commanddemo. command {public class executecommandaction: triggeraction <frameworkelement> {public static readonly dependencyproperty commandnameproperty = dependencyproperty. register ("commandname", typeof (string), typeof (executecommandaction), null); public static readon Ly dependencyproperty commandparameterproperty = dependencyproperty. register ("commandparameter", typeof (object), typeof (executecommandaction), null); protected override void invoke (object parameter) {If (associatedobject = NULL) return; icommand command = NULL; var datacontext = associatedobject. datacontext; foreach (VAR info in datacontext. getType (). getproperties (bindingflags. public | bindingfl AGS. instance) {If (iscommandproperty (Info) & string. equals (info. name, commandname, stringcomparison. ordinal) {command = (icommand) info. getvalue (datacontext, null); break;} If (command! = NULL) & command. canexecute (commandparameter) {command. execute (commandparameter) ;}} Private Static bool iscommandproperty (propertyinfo property) {return typeof (icommand ). isassignablefrom (property. propertytype);} Public String commandname {get {return (string) getvalue (commandnameproperty);} set {setvalue (commandnameproperty, value );}} public object commandparameter {get {return getvalue (commandparameterproperty);} set {setvalue (commandparameterproperty, value );}}}}

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.