
94 Using ActionScript
Using ActionScript in Flex applications
When you write a Flex application, you use MXML to lay out the user interface of your
application, and you use ActionScript to handle the user interactions with the application.
You can use a variety of methods to mix ActionScript and MXML. You can include inline
ActionScript in MXML, or supplement a codeless MXML document with an external
ActionScript controller class.
To use ActionScript in your Flex applications, you can add script blocks using the
<mx:Script> tag or include external ActionScript files. In addition, you can import external
class files or entire packages of class files for use by your MXML application. Flex extracts the
ActionScript and creates a class file, which is linked to the final SWF file. For more
information, see Flex 2 Developer’s Guide.
Using ActionScript blocks in MXML files
ActionScript blocks can contain ActionScript functions and variable declarations when used
in MXML applications.
Statements and expressions are only allowed if they are wrapped in a function. In addition,
you cannot define new classes or interfaces in
<mx:Script> blocks. All ActionScript in the
blocks is added to the enclosing file’s class when Flex compiles the application.
When using an
<mx:Script> block, you must wrap the contents in a CDATA construct. This
prevents the compiler from interpreting the contents of the script block as XML, and allows
the ActionScript to be properly generated. As a result, Adobe recommends that you write all
your
<mx:Script> open and close tags as the following example shows:
<mx:Script>
<![CDATA[
...
]]>
</mx:Script>
The script within a given <mx:Script> tag is accessible from any component in the MXML
file. The
<mx:Script> tag must be located at the top level of the MXML file (within the
Application tag or other top-level component tag). You can define multiple script blocks in
your MXML files, but you should try to keep them in one location to improve readability.
NOTE
In general, Adobe recommends that you import ActionScript class files when possible
rather than use
<mx:Script> blocks in your MXML files or include snippets of
ActionScript code from multiple files.
Kommentare zu diesen Handbüchern