How to make instance of mxml and pass parameters?
i want to create instance of mxml (in my case EventList) and pass parameters. My Event List is a list of panels so I want to pass parameters and generate dynamically n number of panels (n-parameter to pass). I have the main app where I have toggle button bar when I click on the first I want for example to generate 3 panels (n=3) on the second button 20 panels (n=20) etc. How can I do this? How can I pass n and what is the best way to s开发者_StackOverflow中文版how the list? I whant to generate the list when I click on the toggle button!
Use xmlnamespace(xmlns) to access the mxml file in your source folder.I created an application which includes xmlns="*" (* means you can access any component in the source folder)to access the myEvenList component. i pass the n value here itself.Check out the example.HTH.
togglePanelCount.mxml
<?xml version="1.0" encoding="utf-8"?>
<mx:Application name="ToggleButtonBar_toggleOnClick_test"
xmlns:mx="http://www.adobe.com/2006/mxml"
layout="vertical"
verticalAlign="top"
backgroundColor="white" xmlns:eventList="*"> <mx:ToggleButtonBar id="toggleButtonBar"
dataProvider="{viewStack}" /><mx:ViewStack id="viewStack"
width="100%"
height="100%">
<eventList:myEventList n="5" id="List1"/>
<eventList:myEventList n="20" id="List2"/>
</mx:ViewStack></mx:Application>
myEvenList.mxml
<?xml version="1.0" encoding="utf-8"?><mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="createPanels()"><mx:Script> <![CDATA[ import mx.controls.Alert; import mx.containers.Panel; public var n:int; public function createPanels():void{ Alert.show("in create panel"); for(var i:int =0 ;i<n;i++){ var panel:Panel = new Panel(); panel.title = "panel"+(i+1); panelList.addChild(panel); } } ]]> </mx:Script><mx:VBox id="panelList" /></mx:Canvas>
PS:Having n as a public attribute itself gives you a way of passing the count of panels.Even in the mx:Script tag in the application, you can instantiate the myEventList object and set the value of n instead of using mxml tags.
精彩评论