Can I have multiple classes in one file in MXML?

I have a class B that will only be used inside class A. However, class A is written as mxml, not actioncript code. Is it possible to nest classes in MXML or add another class after the root tag in the same .mxml file? Clarification: I want both classes written in MXML to be in the same file, but I could not find anything in the Adobe documentation that indicated how to do this.

+4
source share
3 answers

No, you cannot define two classes in one MXML file, but you can have the same package (namespace) for both classes and make classB internal , therefore it is available only for classes inside this package.

+4
source

I believe you are looking for the fx: Component tag, which allows you to define a new MXML document in 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> 
+2
source

If nested classes require several levels of inheritance, and the alternative to <fx: Component> (mentioned in the 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 ... /> 

<fx: Library> should be at the top of the MXML file. This syntax allows several nested class definitions in a string, and each of them can extend the previous one through inheritance.

0
source

All Articles