Flex 4:创建了事件监听器,但没有被调用?

时间:2010-09-13 16:35:52

标签: flex actionscript-3 actionscript flex4

我正在尝试调用我在另一个组件中创建的事件。我已经在我的方法中添加了trace(),所以我可以看到被调用的内容。正在调用除事件侦听器(myEvent)之外的所有内容。有人能告诉我为什么这样吗?

非常感谢任何帮助。提前谢谢。

// TestApp.mxml (application)
<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
                       xmlns:s="library://ns.adobe.com/flex/spark" 
                       xmlns:mx="library://ns.adobe.com/flex/mx"
                       xmlns:com="com.*"
                       creationComplete="initApp()">
    <fx:Script>
        <![CDATA[
            import com.MyPopUp;

            import mx.managers.PopUpManager;
            protected function initApp():void
            {
                var popUp:MyPopUp = new MyPopUp();    

                PopUpManager.addPopUp(popUp, this);
            }    
        ]]>
    </fx:Script>
    <com:MyComp/>
</s:WindowedApplication>

// MyComp.mxml (component)
<?xml version="1.0" encoding="utf-8"?>
<s:VGroup xmlns:fx="http://ns.adobe.com/mxml/2009" 
          xmlns:s="library://ns.adobe.com/flex/spark" 
          xmlns:mx="library://ns.adobe.com/flex/mx"
          width="100%" height="100%"
          creationComplete="initComp()">
    <fx:Script>
        <![CDATA[
            import mx.controls.Alert;
            import mx.events.DynamicEvent;

            protected function initComp():void
            {
                trace('init MyComp()');

                this.addEventListener('myEvent', myEvent);
            }

            private function myEvent(event:DynamicEvent):void
            {
                trace('myEvent()');

                Alert.show('Event Called!', 'Success');
            }
        ]]>
    </fx:Script>
</s:VGroup>

// MyPopUp.mxml (component)
<?xml version="1.0" encoding="utf-8"?>
<s:Group xmlns:fx="http://ns.adobe.com/mxml/2009" 
         xmlns:s="library://ns.adobe.com/flex/spark" 
         xmlns:mx="library://ns.adobe.com/flex/mx" width="100" height="100">
    <fx:Script>
        <![CDATA[
            import mx.events.DynamicEvent;
            import mx.managers.PopUpManager;

            private function call(event:MouseEvent):void
            {
                trace('call()');

                PopUpManager.removePopUp(this);

                var evt:DynamicEvent = new DynamicEvent('myEvent');
                evt.value1 = '1234';

                dispatchEvent(evt);
            }
        ]]>
    </fx:Script>
    <s:Button click="call(event)" label="Call Event"/>
</s:Group>

1 个答案:

答案 0 :(得分:2)

MyComp和MyPopup不在同一个显示列表层次结构中,因此没有看到冒泡事件。

如果您希望以这种方式跨组件发送消息,请考虑使用某种全局事件调度程序,使用两个组件之间的共享模型来查看数据更改。