我有一个flex应用程序,其中子项在父项内调度事件:
子:
<fx:Metadata>
[Event(name="myEvent", type="flash.events.Event")]
</fx:Metadata>
<fx:Script>
<![CDATA[
dispatchEvent(new Event("myEvent"));
]]>
</fx:Script>
父母:
<comp:poppy id="myPopup" myEvent="myEventStuff(event);" />
我已经更改了我的代码,因此我在actionscript中创建了myChild,而不是如图所示的mxml标记。如何在actionscript中添加事件监听器?
public function showPoppy(evt:MouseEvent):void {
var myPopup:poppy = PopUpManager.createPopUp(this, poppy, true) as poppy;
PopUpManager.centerPopUp(myPopup:poppy );
myPopup:poppy.includeInLayout = true;
myPopup:poppy.visible = true;
// how do I add the event listener?
}
答案 0 :(得分:0)
你可以这样做的一种方法是将poppy类中的“myEvent”字符串分解为poppy类中的静态字符串:
public static const MY_EVENT:String = "myEvent";
然后,您的ActionScript组件可以从“myPopup”实例中侦听此事件:
myPopup.addEventListener(poppy.MY_EVENT, handleMyEvent);
答案 1 :(得分:0)
只是一个细节,你不应该把一些裸代码放进去。喜欢这样的东西:
<s:Group ... creationComplete="onCreationComplete">
<fx:Metadata>
[Event(name="myEvent", type="flash.events.Event")]
</fx:Metadata>
<fx:Script>
<![CDATA[
private function onCreationComplete(e:Event):void
{
dispatchEvent(new Event("myEvent"));
}
]]>
</fx:Script>