Search code examples
actionscript-3flashapache-flexflex4flex4.6

How do you get the namespace of a component?


I'm trying to write CSS stylesheet dynamically but Flex 4 requires that you declare the namespace in the Style markup.

If I have a Spark Button or MX Button class or class instance how would I get the namespace of that button?

So far I've tried this:

var className:Object = getQualifiedClassName(myButton);
var ButtonClass:Object = ApplicationDomain.currentDomain.getDefinition(className);
var button:Object = new ButtonClass();

With that information I can write this:

<fx:Style>

    myButton {
        color: red;
    }

</fx:Style>

I need to create this:

<fx:Style>
    @namespace s "library://ns.adobe.com/flex/spark";
    @namespace mx "library://ns.adobe.com/flex/mx";

    s|Button {
        color: red;
    }

</fx:Style>

I would like to be able to get this information at runtime but design time may be acceptable as well.


Solution

  • It looks like I can't get it at runtime but I can get it XML files from the SDK directory and create a table of classes and their namespace.

    In $FlashBuilder/sdks/4.6.0/frameworks/flex-config.xml there is a node called namespaces that contain the URI of the namespace and then a reference to the XML file that contains a list of classes in that namespace.

      <!-- Specify a URI to associate with a manifest of components for use as MXML -->
      <!-- elements.                                                                -->
         <namespace>
            <uri>http://ns.adobe.com/mxml/2009</uri>
            <manifest>mxml-2009-manifest.xml</manifest>
         </namespace>
         <namespace>
            <uri>library://ns.adobe.com/flex/spark</uri>
            <manifest>spark-manifest.xml</manifest>
         </namespace>
         <namespace>
            <uri>library://ns.adobe.com/flex/mx</uri>
            <manifest>mx-manifest.xml</manifest>
         </namespace>
         <namespace>
            <uri>http://www.adobe.com/2006/mxml</uri>
            <manifest>mxml-manifest.xml</manifest>
         </namespace>
         <namespace>
            <uri>http://flex.apache.org/ns</uri>
            <manifest>apache-manifest.xml</manifest>
         </namespace>
         <namespace>
            <uri>http://flex.apache.org/experimental/ns</uri>
            <manifest>experimental-manifest.xml</manifest>
         </namespace>
      </namespaces>
    

    I can use that information and the MXML document to add the necessary namespace to each node.