2010-11-12 305 views
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圖像隨機出現,無論「文件」是否存在。我究竟做錯了什麼?

回答

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; 
} 
+0

您設置的IOErrorEvent是實現它的方法,但在您的情況下,它不能按照您的方式工作。因此,要進行調試,您需要縮小可能會破壞腳本的區域。首先要做的是檢查你是否真的加載了一個文件(哪個Event.Complete會爲你確認),因爲如果你認爲你是,但你不是,那麼這只是一個用戶錯誤而不是代碼錯誤。 – Aesphere 2010-11-12 12:59:20

+0

這取決於,如果您在UIComponent上偵聽creationComplete,但是您正在將數據加載到該UIComponent,那麼creationComplete事件將在組件創建時觸發,而不是在數據加載完成時觸發。您將需要另一個事件偵聽器來偵聽何時將數據加載到數據obj/var中。如果數據存在,即在創建組件之前嵌入數據,那麼data.File不應該爲空。 – Aesphere 2010-11-14 09:37:33