作者:篱边_水祭 | 来源:互联网 | 2023-05-18 23:40
一个as类中,当数据处理完毕后发送一个finish的事件,然后希望在主程序applicatioin中监听并接受到这个事件,进行后续处理,源代码大致如下:Network.as:packa
一个as类中,当数据处理完毕后发送一个finish的事件,然后希望在主程序applicatioin中监听并接受到这个事件,进行后续处理,源代码大致如下:
Network.as:
package control
{
import flash.events.Event;
import flash.events.EventDispatcher;
[Event(name="finish", type="flash.events.Event")]
public class Network extends EventDispatcher
{
public function Process():void
{
....
DispatchFinishEvent();
}
public function DispatchFinishEvent():void
{
var e:Event = new Event("finish");
var ret:Boolean = this.dispatchEvent(e);
trace(ret);
}
}
}
Main.mxml:
import control.Network;
public function initApp():void
{
addEventListener("finish", showAll);
var testNetwork:Network = new Network();
testNetwork.Process();
}
public function showAll():void
{
trace("success receive");
}
]]>
可以看到,DispatchFinishEvent()函数是运行了的,但是无论如何都不能接收到finish事件,showAll函数进不去,试图在Application标签里放入finish="showAll()",会报错显示无法找到finish这个属性,这是怎么回事?帮我看看,多谢了!
5 个解决方案
先注册事件.
这样是不行的.
你是先监听,在注册.
我在Network.as文件中的这句
[Event(name="finish", type="flash.events.Event")]
不是注册事件吗?
那应该怎么修改才能先注册再监听呢? 多谢了!
layout="absolute"
min
min>
import control.Network;
import mx.controls.Alert;
// private var testNetwork:Network=new Network();
public function initApp():void
{
var testNetwork:Network=new Network();
testNetwork.addEventListener("finish", showAll); testNetwork.Process();
}
public function showAll(event:Event):void
{
Alert.show("success receive");
}
]]>
id="btn"
y="196"
label="Button"
click="initApp()"/>
package control
{
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.IEventDispatcher;
[Event(name="finish", type="flash.events.Event")]
public class Network extends EventDispatcher
{
public function Process():void
{
this.dispatchEvent(new Event("finish"));
}
}
}