扩展Event类,自定义事件MenuButtonEvent.CLICK_MBTN来传递消息,。
效果:当点击flash第一步,flex第一步时主程序的text文本上都会显示你所按下的内容。
首先是主程序main.mxml:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" xmlns:tree="tree.*" creationComplete="initApp()" >
<mx:Script>
<![CDATA[
import events.MenuButtonEvent;
private function initApp():void{
itemCanvas.addEventListener(MenuButtonEvent.CLICK_MBTN,clickHandler);
}
private function clickHandler(evt:MenuButtonEvent):void{
myTxt.text="你点击"+evt.clickItem;
}
]]>
</mx:Script>
<tree:myCanvas width="303" height="179" id="itemCanvas"/>
<mx:HBox x="24" y="242" width="100%" height="39">
<mx:Label text="你按下了:" height="38"/>
<mx:Text height="37" width="273" id="myTxt"/>
</mx:HBox>
</mx:Application>
其次,myCanvas.mxml(在tree文件夹下):
<?xml version="1.0" encoding="utf-8"?>
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" width="400" height="300" creationComplete="initApp()" fontSize="12">
<mx:Script>
<![CDATA[
import mx.events.MenuEvent;
import events.MenuButtonEvent;
private var mXML:XMLList=
<>
<node label="flash书">
<node label="flash第一步"/>
<node label="actionscrpt3殿堂之路"/>
</node>
<node label="flex书">
<node label="flex第一步"/>
<node label="完全手册Flex3.0RIA开发详解"/>
</node>
<node label="帮助">
<node label="作者"/>
<node label="答疑"/>
<node label="勘误"/>
</node>
</>
private function initApp():void{
popMBtn.dataProvider=mXML;
}
private function change(event:MenuEvent):void{
dispatchEvent(new MenuButtonEvent(event.label));
}
]]>
</mx:Script>
<mx:PopUpMenuButton x="62" y="95" label="PopUpMenuButton" id="popMBtn" labelField="@label" itemClick="change(event)"/>
</mx:Canvas>
最后是:MenuBtnEvent.as(在events文件夹下):
package events
{
import flash.events.Event;
public class MenuButtonEvent extends Event
{
public var clickItem:String;
public static const CLICK_MBTN:String="Click_Btn";
public function MenuButtonEvent(labels:String)
{
clickItem=labels;
super(MenuButtonEvent.CLICK_MBTN,false,false);
}
}
}