views:

3336

answers:

3

I have an actionscript file that defines a class that I would like to use inside a Flex application.

I have defined some custom controls in a actionscript file and then import them via the application tag:


<mx:Application
    xmlns:mx="http://www.adobe.com/2006/mxml"
    xmlns:scorecard="com.apterasoftware.scorecard.controls.*"
...
</mx:Application>

but this code is not a flex component, rather it is a library for performing math routines, how do I import this class?

+5  A: 

You'd need to import the class inside a script tag.

<mx:Application
  xmlns:mx="http://www.adobe.com/2006/mxml"&gt;
  <mx:Script>
    import com.apterasoftware.scorecard.controls.*;
    // Other imports go here

    // Functions and other code go here
  </mx:Script>

  <!-- Components and other MXML stuff go here -->
  <mx:VBox>
    <!-- Just a sample -->
  </mx:VBox>
</mx:Application>

Then you'll be able to reference that class anywhere else in your script tag. Depending on how the class is written you may not be able to use binding within the MXML, but you could define your own code to handle that.

Namespace declarations are only used to import other MXML components. AS classes are imported using the import statement either within a Script block or another AS file.

Herms
A: 

@Herms,

ah...I tried this but had a misspelled package...thanks!

mmattax
A: 

@Herms: To clarify a little, namespace declarations can be used to "import" AS classes as well, when you're going to instantiate them using MXML.

For example, consider having a custom visual component you've written entirely in AS, let's say com.apterasoftware.scorecard.controls.MathVisualizer. To use it within MXML:

<mx:Application
    xmlns:mx="http://www.adobe.com/2006/mxml"
    xmlns:aptera="com.apterasoftware.scorecard.controls.*">

    <aptera:MathVisualizer width="400" height="300" />
</mx:Application>
Niko Nyman