FLEX: How to reference a MXML class from an AS3 external class file?
I have a mxml class where I place a list and a couple of buttons.
I'd like two things:
- To access to the list from a class that is an external file.
- To add my as3 class as child (visual element) since I need to get the "stage" (global property).
I woudnt't like to embed too much code into mxml th开发者_如何学JAVArough <![CDATA[]]>
.
So, example of mxml
<?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"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="800" minHeight="600" width="800" visible="true">
<fx:Style source="Main.css"/>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Script>
<![CDATA[
private var menuController:CMenuController= new CMenuController();
]]>
</fx:Script>
<s:List x="598.35" y="100.55" width="178" height="324" id="ListBox">
<s:layout>
<s:VerticalLayout/>
</s:layout>
</s:List>
</s:Application>
Thus, I'd like to access to ListBox from CMenuController as I was typing in CDATA. Besides, I need to be able to add Sprites and Shapes trough addChild() method in CMenuController.
You have to add Sprites and Shapes to UIComponent first and then add this to the Flex code.
To access the Flex code from external as3 class have a reference in the as3 class. Like this:
menuController.listReference = LISTID;
I don't think you get the concept of separating visual elements from the application logic. What you're trying to accomplish is considered very bad form since it makes for spaghetti code.
What you should do is instead use a data-driven approach by create an ArrayCollection
of whatever you want to show in the List (which could be a property of MenuController). Then add a custom item renderer to do whatever you need it to. Kind of like this:
<s:List dataProvider="{menuController.yourListData}" itemRenderer="YourCustomItemRenderer" />
Within the item renderer, you can display whatever you want depending on the data of yourListData
. I recommend you read on how item renderer works as well as try to find examples of data driven Flex applications.
精彩评论