« [Flex Tips] Flex Tips Introduction | Main | Verizon Customer Service EPIC FAIL »

February 05, 2009

[Flex Tips] Importing MXML into ActionScript and Vice Versa

I get this question a lot from Flash peps first coming into Flex so figured I should make this my first real tip.

At compile time, MXML files actually get converted to ActionScript so the only real difference between ActionScript and MXML is the syntax. Under the hood, it’s all the same.

To import just rock the import statement like you would for any other class:

import package.ClassName;

For example: Say you have an MXML file: com.foo.FooBar.mxml; your import statement would look like:

import com.foo.FooBar;

Similarly, to import a class or another MXML file into an MXML file you would use the xmlns parameter:

<mx:RootNode xmlns:namespace="package">

Using the FooBar example:

<mx:Application
xmlns:mx=http://www.adobe.com/2006/mxml
xmlns:fb="com.foo.*">

From there on out, you can reference your FooBar class or any other file inside the com.foo package (MXML or AS) by typing in the namespace you created in the xmlns parameter

<fb:FooBar>

Something to watch out for (and still bites me in the rear on occasion) is to make sure you import using the wildcard (.*) at the end of package in the MXML file.

More to come!

-erik

Posted by erikbianchi at February 5, 2009 08:33 AM

Comments

Post a comment




Remember Me?