MXML is an XML-based markup language for conveniently defining user interfaces and data binding using the Flex framework. MXML files can include ActionScript inside <mx:Script> tags - just like you can have javascript in an html file.
The Flex compiler converts MXML markup to ActionScript-3 code before compiling it to SWF / SWC. Most of the things you do in MXML can also be done using ActionScript, but this will require more lines of code.
The MXML file creates an ActionScript class with the same name that extends the class corresponding to the root tag of the mxml file. For example, the following code in MyCanvas.mxml generates the MyCanvas class, which extends the Flex Canvas class.
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" width="200" creationComplete="init(event)"> <mx:Label text="{someVar}" id="theLabel"/> <mx:Script> <![CDATA[ [Bindable] public var someVar:String; public function init(e:Event):void { someVar = "Created"; } ]]> <mx:Script> </mx:Canvas>
It is equivalent to MyCanvas.as , which contains:
package { import mx.containers.Canvas; import mx.controls.Label; import mx.binding.utils.BindingUtils; [Bindable] public var someVar:String; [Bindable] public var theLabel:Label; public class MyCanvas extends Canvas { this.width = 200; this.addEventListener(FlexEvent.CREATION_COMPLETE, init); } public function init(e:Event):void { someVar = "Created"; } override protected function createChildren():void { theLabel = new Label(); addChild(theLabel); BindingUtils.bindProperty(theLabel, "text", this, "someVar"); } }
If you look at the code of any Flex class (for example, UIComponent , Canvas , etc.), you will see that these are all .as files, not .mxml .
Amarghosh
source share