Search code examples
apache-flexmxml

How to include mxml componnent from a Flex library into a different Flex project?


I created a Flex Library Project which contains an .MXML file with one componnent (a button). It's called Button.mxml in myLibrary.views package.

Then i created a different Flex Mobile project in which i want to include the button from my library project. This project refers the previously created library.

I can't find an answer on how to achieve this.

Can i add it somehow like an mxml componnent?

   <?xml version="1.0" encoding="utf-8"?>
  <         s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
           xmlns:s="library://ns.adobe.com/flex/spark" 

           applicationDPI="240">
<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>

<fx:Script>
    <![CDATA[
        import myPackage.views.Button; // this is the package containing Button.mxml

    ]]>
</fx:Script>
 <myPackage.Views.Button id="myButton" /> // is this possbile somehow ???

     </s:Application>

Thank you!


Solution

  • First be sure to add the other project to your library path. You can do this by adding the generated SWC to the libs folder or by adding it to the library path. IF both projects are in the same Flash Builder workspace you can use the "Add project" button on the library tab.

    Next, to use your new Button file in MXML, you must import the namespace in the top level MXML tag; something like this:

     <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
                   xmlns:s="library://ns.adobe.com/flex/spark" 
                   xmlns:myPackage="myPackage.views.*"
                   applicationDPI="240">
    

    The syntax is very similar to what Flex does for their own packages. Except you probably haven't gone through the process of creating a namespace URL, so you use the package name. Not creating a namespace URL is fine in most cases it is more headache than it is worth.

    Then you can use your custom button like this:

    <myPackage:Button id="myButton" />
    

    This follows the same syntax as other Flex components; which is namespace:ComponentName.

    You cannot use the Component in MXML based on an import statement in ActionScript; but you had the right idea.