0
我有一個DataGrid瓦特/自定義項目渲染,如下:IOErrorEvent ....我做錯了什麼?
<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圖像......然而,當我編譯&運行上面的代碼,.png圖像隨機出現,無論「文件」是否存在。我究竟做錯了什麼?
您設置的IOErrorEvent是實現它的方法,但在您的情況下,它不能按照您的方式工作。因此,要進行調試,您需要縮小可能會破壞腳本的區域。首先要做的是檢查你是否真的加載了一個文件(哪個Event.Complete會爲你確認),因爲如果你認爲你是,但你不是,那麼這只是一個用戶錯誤而不是代碼錯誤。 – Aesphere 2010-11-12 12:59:20
這取決於,如果您在UIComponent上偵聽creationComplete,但是您正在將數據加載到該UIComponent,那麼creationComplete事件將在組件創建時觸發,而不是在數據加載完成時觸發。您將需要另一個事件偵聽器來偵聽何時將數據加載到數據obj/var中。如果數據存在,即在創建組件之前嵌入數據,那麼data.File不應該爲空。 – Aesphere 2010-11-14 09:37:33