Can I have multiple classes in one file in MXML?
I have a classB that will only be used inside classA. However, classA is written as mxml, not actionscript code. Is it possible to nest classes 开发者_如何学Goin MXML or add another class after the root tag in the same .mxml file? Clarification: I want both classes written in MXML within the same file, but I couldn't find anything in the Adobe documentation that specified how.
No, you can't define two classes in one MXML file, but you can have the same package (namespace) for both classes and make classB internal
, so its only visible for classes within that package.
I believe you are looking for the fx:Component tag that allows you to define a new MXML document within an existing MXML document:
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:local="*">
<fx:Declarations>
<fx:Component className="MyMXMLClass1">
<s:Group>
<s:Button label="MyMXMLClass1" />
</s:Group>
</fx:Component>
<fx:Component className="MyMXMLClass2">
<s:Group>
<s:Button label="MyMXMLClass2" />
</s:Group>
</fx:Component>
</fx:Declarations>
<s:VGroup>
<local:MyMXMLClass1 />
<local:MyMXMLClass2 />
</s:VGroup>
</s:Application>
If multiple levels of inheritance are required in the nested classes, and alternative to <fx:Component> (mentioned in a previous answer) is to use <fx:Library> for example:
<fx:Library>
<fx:Definition
name="MyClass"
>
<s:Group>
...
<s:/Group>
</fx:Definition>
</fx:Library>
...
<!-- Use MyClass later in the file. -->
<fx:MyClass ... />
The <fx:Library> must be at the top of the MXML file. This syntax allows for several nested class definitions in a row, and each can extend the previous via inheritance.
精彩评论