我创建了一个自定义事件,我想用它在两个模块之间传递一个字符串。事件如下所示:
package com.mypackage.events
{
import flash.events.Event;
public class ThumbDeleteEvent extends Event
{
public static const THUMBS_DELETED:String = "thumbsDeleted";
public var files:String;
public function ThumbDeleteEvent(type:String, files:String)
{
super(type);
this.files = files;
}
// Override the inherited clone() method.
override public function clone():Event {
return new ThumbDeleteEvent(type, files);
}
}
}
在一个模块中,我像这样发送事件:
parentApplication.dispatchEvent(new ThumbDeleteEvent("parentApplication.thumbsDeleted", files));
在另一个模块中,我像这样监听事件:
public function init():void {
parentApplication.addEventListener("parentApplication.thumbsDeleted", onThumbsDelete);
}
如果我使用 ThumbsDeleteEvent 作为传入侦听器函数的类型,如下所示:
public function onThumbsDelete(evt:ThumbDeleteEvent):void{
trace("thumb delete event for thumbs: "+evt.files);
}
我收到以下错误:
TypeError: Error #1034: Type Coercion failed: cannot convert com.mypackage.events::ThumbDeleteEvent@26748a31 to com.mypackage.events.ThumbDeleteEvent.
如果我只是使用 Event 作为传入侦听器函数的类型,如下所示:
public function onThumbsDelete(evt:ThumbDeleteEvent):void{
if(evt is ThumbDeleteEvent){
trace("thumb delete event for thumbs: "+(evt as ThumbDeleteEvent).files);
}else{
var type:XML = describeType(evt);
trace(type.toXMLString());
}
}
它工作但不认为它是 ThumbDeleteEvent 类型类(它命中 else 语句)描述类型的 xml 输出说它的类型是:
type name="com.mypackage.events::ThumbDeleteEvent"
这里发生了什么?如果我在调试器中放置断点,它会说事件是 ThumbDeleteEvent,我可以看到 files 参数及其正确的???