IOErrorEvent ....我做错了什么?

时间:2010-11-12 03:51:39

标签: flex actionscript-3

我有一个带有自定义项呈示器的数据网格,如下所示:

<mx:AdvancedDataGridColumn dataField="file">
<mx:itemRenderer>
<fx:Component>
<mx:HBox paddingLeft="2">
<fx:Script>
<![CDATA[

import mx.core.BitmapAsset;
[Embed(source="components/download.png")]
[Bindable]
public var imgCls:Class;

public function IOErrorEventExample():void {
var loader:URLLoader = new URLLoader();
loader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
var request:URLRequest=new URLRequest("http://www.site.com/"+data.file);
loader.load(request);
}

private function ioErrorHandler(event:IOErrorEvent):void {
if ( String(event) != null ){
// load the itemrenderer image here if the file exists on our server
var imgObj:BitmapAsset = new imgCls() as BitmapAsset;
myImage.source=imgObj;

}
else {
// don't load the itemrenderer image if the file doesn't exist yet
}}                                      
]]>
</fx:Script>
<mx:Image id="myImage" creationComplete="IOErrorEventExample();"/>
</mx:HBox>
</fx:Component>
</mx:itemRenderer>
</mx:AdvancedDataGridColumn>

所以,如果我的服务器上有实际的文件,我想显示download.png图像...但是,当我编译&amp;运行上面的代码,.png图像随机出现..无论“文件”是否存在。我做错了什么?

1 个答案:

答案 0 :(得分:1)

您需要在关闭CDATA标记之前通过添加最后一个大括号来关闭ioErrorHandler函数

不能肯定会解决问题,逻辑看起来是正确的。您可以尝试添加Event.COMPLETE事件侦听器,以确保文件确实存在并正在加载。

编辑:
您可以将此事件处理函数简化为此,因为:
1。只有在调度IOErrorEvent时才会调用此函数,因此调用它时总会出现一个事件对象。 (不需要if语句)
2。即使您将if语句保留在其中,也不需要调用else语句,因为如果文件存在,则不会调用此函数。

private function ioErrorHandler(event:IOErrorEvent):void 
{
    var imgObj:BitmapAsset = new imgCls() as BitmapAsset;
    myImage.source=imgObj;
}