1
我在調度自定義事件時遇到問題,但偵聽器未收到它(即下面代碼中的myHandler())。如果我把所有東西放在一個mxml文件中,它就可以工作。當我把責任分開到不同的課程時,它就會失敗。我不清楚我缺少什麼。未捕獲自定義事件 - 爲什麼?
任何幫助你將不勝感激。
這裏是我的代碼(在ViewModel.as更新()的入口點):
ChangeEvent.as
import flash.events.Event;
public class ChangeEvent extends Event
{
public function ChangeEvent(type:String, bubbles:Boolean=false, cancelable:Boolean=false)
{
super(type, bubbles, cancelable);
// Set the new property.
this.isEnabled = isEnabled;
}
// Define static constant.
public static const ENABLE_CHANGED:String = "enableChanged";
// Define a public variable to hold the state of the enable property.
public var isEnabled:Boolean;
// Override the inherited clone() method.
override public function clone():Event {
return new ChangeEvent(type, isEnabled);
}
}
Model.as
public class Model extends EventDispatcher
{
private function TriggerEvent():void
{
var eventObj:ChangeEvent = new ChangeEvent(ChangeEvent.ENABLE_CHANGED);
dispatchEvent(eventObj);
}
}
ViewModel.as
public class ViewModel
{
import mx.controls.Alert;
import ChangeEvent;
private var model:Model;
public function ViewModel()
{
model = new Model();
addEventListener(ChangeEvent.ENABLE_CHANGED, myHandler);
}
public function update():void {
model.LoadData();
}
private function myHandler(event:Event):void {
Alert.show("An event occurred.");
}
}
是否必須在ViewModel.as中「註冊」事件,類似於mxml中的元數據標記? 例如 [事件(名稱=「enableChange」,類型=「的ChangeEvent」)]
謝謝你。這工作(大聲笑 - 如此明顯) – TERACytE 2010-01-16 03:54:28