我使用actionscript中的自定義屬性創建了自定義事件。我可以使用自定義屬性分派一個自定義事件(在Main.mxml中),但我無法在我的應用程序的任何其他位置監聽此事件。我試圖在自定義組件中聽這個事件。收聽flex中的自定義事件
當我調試時,我可以確認事件得到正確調度與正確的值。
片段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)
}
你嘗試使用creationComplete =「的init() 「而不是?我認爲當初始化事件被觸發時,this.parentApplication可能不存在(所以偵聽器永遠不會添加任何東西) – 2012-12-29 19:45:20