Log In  
 
  Getting Started  
----------------------------------------

Resources

 

MXML vs ActionScript 3.0

Flex components can be added to an application using MXML or ActionScript 3.0. Both code examples below display the same results.

previous page next page

MXML

Components are implicitly initialized when the user runs the application.

  • The Application class is a container
  • Components are added to an application as children objects.
  • MXML is compiled to ActionScript 3.0
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
    backgroundColor="#FFFFFF"
    backgroundAlpha="0">

    <mx:Image source="assets/animals03.jpg" />

    <mx:Label text="Photographed by Elsie Weil" />

</mx:Application>

ActionScript 3.0

Components are created and added to the display though an explicit call to the addControls() function when the user runs the application.

You need to add an initialize listener to the application object to call the function that adds the components (initialize="addControls()").
You need to explicitly attach the components to application object (this.addChild(photo))

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
    backgroundColor="#FFFFFF"
    backgroundAlpha="0"
    initialize="addControls()">

	<mx:Script>
	   <![CDATA[
            import mx.controls.Image;
            import mx.controls.Label;

            private function addControls():void {
                createImage();
                createPhotographer();
            }

            private function createImage():void {
                var photo:Image = new Image();
                photo.source = "assets/animals03.jpg";
                this.addChild(photo);
            }

            private function createPhotographer():void {
                var photographer:Label = new Label();
                photographer.text = "Photographed by Elsie Weil";
                this.addChild(photographer);
            }

	   ]]>
	</mx:Script>

</mx:Application>

Rendered Samples

Both applications render the same even though one adds components using MXML and the other using ActionScript 3.0 code.

MXML

ActionScript 3.0

previous page next page

In the functions createImage() and createPhotographer()
what does the term "this" refer to?
Is it the application instance, or <mx:Script> create an instance of some class that "this" refers to?

Comment: Posted by Ben Coman at Jun 27, 2008 14:50

The 'this' keyword is a reference to a method's containing object. When a script executes, the 'this' keyword references the object that contains the script. Inside a method body, the 'this' keyword references the class instance that contains the called method.

Source: http://livedocs.adobe.com/flex/3/langref/statements.html#this

Comment: Posted by Martin Harrigan at Jul 18, 2008 11:28

使用ActionScript添加组件可以更灵活的控制组件的现实方式及显示时间.

Comment: Posted by Moogle Lee at Dec 13, 2011 20:21
Added by Mark Nichoson, last edited by matthew horn on Apr 22, 2008  (view change)

Labels:

Enter labels to add to this page:
Wait Image 
Looking for a label? Just start typing.