Flex 3 Customevent未被派遣

时间:2010-02-06 01:58:57

标签: flex actionscript-3

我有一个名为DrawPlaybook的函数,它可以监听两个事件,一个鼠标点击事件和一个自定义事件。

public function DrawPlaybook(...):void
{
    //...... other stuff
    panel.addEventListener(MouseEvent.CLICK,
        function(e:MouseEvent){onClickHandler(e,this.panel)});
    panel.addEventListener(CustomPageClickEvent.PANEL_CLICKED,
        onCustomPanelClicked);
}

我打算从“onClickHandler”中调用自定义事件,如下所示:

public function onClickHandler(e:MouseEvent,panel):void
{
    var eventObj:CustomPageClickEvent = new CustomPageClickEvent("panelClicked");
    eventObj.panelClicked = panel;
    dispatchEvent(eventObj);
}

private function onCustomPanelClicked(e:CustomPageClickEvent):void {
    Alert.show("custom click");
}

这是CustomPageClickEvent的类定义:

package
{
    import flash.events.Event;

    import mx.containers.Panel;

    public class CustomPageClickEvent extends Event
    {
        public var panelClicked:Panel; 

        // Define static constant.
        public static const PANEL_CLICKED:String = "panelClicked";

        public function CustomPageClickEvent(type:String){
            super(type);
            //panelClicked = panel;
        }

        // Override the inherited clone() method.
        override public function clone():Event {
            return new CustomPageClickEvent(type);
        }

        public function getPanelSource():Panel{
            return panelClicked;
        }
    }
}

问题是“onCustomPanelClicked”永远不会被调用。如果您发现我遗漏的任何内容,请告诉我。

1 个答案:

答案 0 :(得分:2)

这是因为您在Panel上注册了CustomPageClickEvent的事件监听器,但是您从DrawPlaybook

发送了它

只需改变一下:

var eventObj:CustomPageClickEvent = new CustomPageClickEvent("panelClicked");
eventObj.panelClicked = panel;
dispatchEvent(eventObj)

到此:

var eventObj:CustomPageClickEvent = new CustomPageClickEvent("panelClicked");
eventObj.panelClicked = panel;
panel.dispatchEvent(eventObj)

...或将事件监听器更改为this.addEventListener(CustomPageClickEvent.PANEL_CLICKED, onCustomPanelClicked);

如果有效,请告诉我。