0

我在 actionscript 中创建了一个带有自定义属性的自定义事件。我可以使用自定义属性调度自定义事件(在 Main.mxml 中),但我无法在我的应用程序的其他任何地方侦听此事件。我正在尝试在自定义组件中收听此事件。

当我调试时,我可以确认事件以正确的值正确分派。

Main.mxml 的片段:

var userid:String = result.charAt(4);
//dispatch a custom event in which we store the userid
var event_userid:MyEvent = new MyEvent(MyEvent.COMPLETE,userid);
dispatchEvent(event_userid);

自定义事件类:

package ownASClass
{
    {
//http://stackoverflow.com/questions/1729470/attaching-a-property-to-an-event-in-flex-as3
    import flash.events.Event;

    public class MyEvent extends Event
    {

        public static const COMPLETE:String = "MyEventComplete";

        public var myCustomProperty:*;

        public function MyEvent(type:String, prop:*) :void
        {
            super(type,true);
            myCustomProperty = prop;

        }

        //override clone() so your event bubbles correctly
        override public function clone() :Event {

            return new MyEvent(this.type, this.myCustomProperty)

        }
    }
}       

}

我在自定义组件中收听此事件:

//init gets called as: initialize="init();
protected function init():void
        {
            //add evt listener for custom event to get userid
            this.parentApplication.addEventListener(MyEvent.COMPLETE,getUserid);
            //my custom component is part of a viewstack defined in main.mxml
        }

protected function getUserid(event:Event):void
        {
            //do something (but this never gets called)

        }
4

3 回答 3

1
protected function init():void
    {
        //add evt listener for custom event to get userid
        this.parentApplication.addEventListener(MyEvent.COMPLETE,getUserid);
        //my custom component is part of a viewstack defined in main.mxml
    }

请尝试将上面的行更改如下 -

this.addEventListener(MyEvent.COMPLETE,getUserid);
于 2012-12-31T08:31:24.317 回答
0

为此使用了一种解决方法,意识到我真的不需要调度自定义事件。在主应用程序中声明了我的用户 ID,并在我的组件中调用它:

public var userid:String = FlexGlobals.topLevelApplication.userid;
于 2012-12-29T21:15:34.300 回答
0

事件通常会“冒泡”从子窗口到父窗口再到应用程序(main.mxml)的链。

http://livedocs.adobe.com/flex/3/html/help.html?content=events_08.html

如果您尝试使用事件来向某个子对象发出信号,则需要将其定位到那里并注册一个事件侦听器。

于 2012-12-29T20:08:37.567 回答