主应用程序为index.mxml(包含加载module的moduleloader控件),被加载的module为mo_test.swf(mo_test.mxml)
在mo_test.mxml中使用timer
mo_test.mxml中:
某函数中{
timer.addEventListener("timer", timerHandler);
timer.start();
}
public function timerHandler(e:TimerEvent):void
{
执行代码A
}
解决方法:
在主应用程序中定义全局变量保存module的状态,即当前
是否被加载。然后在timerHandler中调用判断
index.mxml中:
public var moduleState:Boolean=false;
.......
//并在加载module前将moduleState设置为true
this.moduleState=true;
.........
//在切换moduleloader中的module时或卸载module时将moduleState设置为false
this.moduleState=false;
mo_test.mxml中:
某函数中{
timer.addEventListener("timer", timerHandler);
timer.start();
}
public function timerHandler(e:TimerEvent):void
{
if(!this.parentApplication.moduleState)
{
timer.stop();
return;
}
执行代码A
}
到现在,当mo_test被卸载以后,timerHandler已经不再执行了。但当再次加载mo_test的时候有可能报错:
错误:
无法将 Object@ce19af1 转换为 mx.messaging.messages.IMessage
具体是什么原因没仔细推敲。
解决方法:在index.mxml中添加代码如下:
import mx.messaging.messages.RemotingMessage;
private var rm:RemotingMessage;//只是定义一下就好了。很莫名奇妙的问题,在module中使用datagrid和tabnavigater时也有些类似的解决方案。