[flex] FLEX4 事件流处理
mfnevi
2011-03-28
最近学习FLEX,在事件流方面遇到点问题,还望各位指点迷津:
情况是这样的,一个容器A中放置了另外的UIComponent组件比如B, A,B同事监听一个自定义事件,比如TTEvent,但是当在B中DispatchEvent后,在冒泡阶段中A没有监听到该事件,举个例子吧 <?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="955" minHeight="600" creationComplete="tt()"> <s:layout> <s:BasicLayout/> </s:layout> <fx:Script> <![CDATA[ import ui.common.event.AnchorPointEvent; public function tt():void { bu1.addEventListener(AnchorPointEvent.ADD, mouseClick, true); c1.addEventListener(AnchorPointEvent.ADD, mouseClick, true); bu1.addEventListener(AnchorPointEvent.ADD, mouseClick, false); c1.addEventListener(AnchorPointEvent.ADD, mouseClick, false); } public function aa():void{ bu1.dispatchEvent(new AnchorPointEvent(AnchorPointEvent.ADD)); } public function mouseClick(event:AnchorPointEvent):void { trace("block pointAddEvent start"); if(event.eventPhase == EventPhase.CAPTURING_PHASE){//添加节点事件在捕获阶段 trace("pointAddEvent CAPTURING_PHASE"); var target:Object = event.currentTarget; trace((target as DisplayObject).name + (event.target as DisplayObject).name); } if(event.eventPhase == EventPhase.AT_TARGET){//添加节点事件在目标阶段 trace("pointAddEvent AT_TARGET"); var target:Object = event.currentTarget; trace((target as DisplayObject).name + (event.target as DisplayObject).name); } if(event.eventPhase == EventPhase.BUBBLING_PHASE){//添加节点事件在冒泡阶段 trace("pointAddEvent BUBBLING_PHASE"); var target:Object = event.currentTarget; trace((target as DisplayObject).name + (event.target as DisplayObject).name); } trace("block pointAddEvent end"); trace(); } ]]> </fx:Script> <fx:Declarations> <!-- 将非可视元素(例如服务、值对象)放在此处 --> </fx:Declarations> <s:BorderContainer x="266" y="44" width="200" height="200" id="c1"> <s:Button x="89" y="108" label="按钮" id="bu1" click="aa()"/> </s:BorderContainer> </s:Application> 其中 public class AnchorPointEvent extends Event { public static const ADD:String = "01";//添加节点 public function AnchorPointEvent(type:String = AnchorPointEvent.ADD, bubbles:Boolean=false, cancelable:Boolean=false) { //TODO: implement function super(type, bubbles, cancelable); } } 但是Debug的结果却是: block pointAddEvent start pointAddEvent CAPTURING_PHASE BorderContainer5Button8 block pointAddEvent end block pointAddEvent start pointAddEvent AT_TARGET Button8Button8 block pointAddEvent end 不知道,该怎么让父容器也触发该事件. |
|
mfnevi
2011-03-28
刚才又找了半天资料,好像自定义事件不支持冒泡阶段吧?不知道是否这样,
还有一个问题,组件自定义事件,一定要等组件,及子组件全部初始化完成么? |
|
remoteJavaSky
2011-07-02
自定义事件,默认貌似就支持冒泡,更不可能不支持了。你这例子非把Flex给弄复杂了
第二个问题都不懂我。事件和组件本身是独立的。 |