2008-08-06 120 views
5

我編寫了一個組件,它顯示文件名,縮略圖並具有一個用於加載/播放文件的按鈕。該組件是綁定到中繼器的數據。我怎樣才能讓按鈕事件觸發主應用程序並告訴它要播放哪個文件?Adob​​e Flex組件事件

回答

1

在您的自定義組件上,您可以收聽按鈕單擊事件,然後生成一個自定義事件,其中包含有關要播放的文件的信息。然後,您可以將事件的bubbles屬性設置爲true,並從您的自定義組件中分派自定義事件。氣泡屬性將使您的事件浮出顯示列表併到達主應用程序。現在在您的主應用程序中,您可以收聽該事件並播放正確的文件。希望這可以幫助。

1

想通了(最終)

定製組件

<?xml version="1.0" encoding="utf-8"?> 
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" x="0" y="0" width="215" height="102" styleName="leftListItemPanel" backgroundColor="#ECECEC" horizontalScrollPolicy="off" verticalScrollPolicy="off"> 
<mx:Script> 
    <![CDATA[ 
     [Bindable] public var Title:String = ""; 
     [Bindable] public var Description:String = ""; 
     [Bindable] public var Icon:String = ""; 
     [Bindable] public var FileID:String = ""; 
     private function viewClickHandler():void{ 
      dispatchEvent(new Event("viewClick", true));// bubble to parent 
     } 
    ]]> 
</mx:Script> 
<mx:Metadata> 
    [Event(name="viewClick", type="flash.events.Event")] 
</mx:Metadata> 
<mx:Label x="11" y="9" text="{String(Title)}" styleName="listItemLabel"/> 
<mx:TextArea x="11" y="25" height="36" width="170" backgroundAlpha="0.0" alpha="0.0" styleName="listItemDesc" wordWrap="true" editable="false" text="{String(Description)}"/> 
<mx:Button x="20" y="65" label="View" click="viewClickHandler();" styleName="listItemButton" height="22" width="60"/> 
<mx:LinkButton x="106" y="68" label="Details..." styleName="listItemLink" height="18"/> 
<mx:HRule x="0" y="101" width="215"/> 

中繼器

<mx:Canvas id="pnlSpotlight" label="SPOTLIGHT" height="100%" width="100%" horizontalScrollPolicy="off"> 
    <mx:VBox width="100%" height="80%" paddingTop="2" paddingBottom="1" verticalGap="1"> 
     <mx:Repeater id="rptrSpotlight" dataProvider="{aSpotlight}">    
      <sm:SmallCourseListItem 
       viewClick="PlayFile(event.currentTarget.getRepeaterItem().fileName);" 
       Description="{rptrSpotlight.currentItem.fileDescription}" 
       FileID = "{rptrRecentlyViewed.currentItem.fileName}"  
       Title="{rptrSpotlight.currentItem.fileTitle}" /> 
     </mx:Repeater> 
    </mx:VBox> 
</mx:Canvas> 

處理功能

private function PlayFile(fileName:String):void{ 
    Alert.show(fileName.toString()); 
}