Adobe Flex组件事件

时间:2008-08-06 18:57:38

标签: flex actionscript-3

我写了一个显示文件名,缩略图的组件,并有一个加载/播放文件的按钮。该组件被数据绑定到转发器。如何才能使按钮事件触发主应用程序并告诉它要播放哪个文件?

2 个答案:

答案 0 :(得分: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());
}

答案 1 :(得分:1)

在自定义组件上,您可以收听按钮单击事件,然后生成一个自定义事件,其中包含有关您要播放的文件的信息。然后,您可以在事件上将bubbles属性设置为true,并从自定义组件中调度自定义事件。 bubbles属性将使您的事件在显示列表中浮动并到达主应用程序。现在,在您的主应用程序中,您可以收听该事件并播放正确的文件。希望这会有所帮助。