0

将自定义事件从“ItemRendered”发送到“主应用程序”时遇到问题,当我单击位于“DataGrid”中的“CheckBox”(ItemRendered)时未调用函数“avatarSelectedHandler()”,我跟着官方 doc adobe 但不幸的是我没有找到解决方案。

在我的代码下方:

Try.mxml(主应用程序)

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
           xmlns:s="library://ns.adobe.com/flex/spark" 
           xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" creationComplete="initApps()">

<fx:Script>
    <![CDATA[
        import entity.Avatar;
        import events.AvatarSelected;
        import mx.collections.ArrayCollection;
        import mx.controls.Alert;

        [Bindable]
        private var listAvatar:ArrayCollection=new ArrayCollection();




        public function initApps():void{
            getAvatar();
               avatarColumn.addEventListener(AvatarSelected.SELECTED_AVATAR,avatarSelectedHandler);
            //avatarGrid.addEventListener(AvatarSelected.SELECTED_AVATAR,avatarSelectedHandler);
            if(avatarColumn.hasEventListener(AvatarSelected.SELECTED_AVATAR))
            Alert.show("EventListener OK ");

        }
        public  function avatarSelectedHandler(event:AvatarSelected):void
        {
            Alert.show("L'evenement est bien traité sur main");
        }

        public function getAvatar():void{
            var date:Date=new Date();

            var avatar1:Avatar=new Avatar();
            avatar1.idAvatar=4562;
            avatar1.pseudo="X";

            var avatar2:Avatar=new Avatar();
            avatar2.idAvatar=659;
            avatar2.pseudo="Y";

            listAvatar.addItem(avatar1);
            listAvatar.addItem(avatar2); 
        }   
    ]]>
</fx:Script>

<s:DataGrid x="158" y="177" width="649" height="194" fontFamily="Times New Roman" id="avatarGrid"
            fontSize="15" requestedRowCount="4" textAlign="center" dataProvider="{listAvatar}">
    <s:columns>
        <s:ArrayList>
            <s:GridColumn dataField="isAvatar" headerText="Id" ></s:GridColumn>
            <s:GridColumn dataField="pseudo" headerText="Pseudo"></s:GridColumn>
            <s:GridColumn  headerText="Selectionner" width="100" itemRenderer="rendered.CheckRendered" id="avatarColumn"></s:GridColumn>


        </s:ArrayList>
    </s:columns>

</s:DataGrid>
</s:Application>

我的 ItemRendered “CheckRendered.mxml”

<?xml version="1.0" encoding="utf-8"?>
<s:GridItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009"
                xmlns:s="library://ns.adobe.com/flex/spark"
                xmlns:mx="library://ns.adobe.com/flex/mx"
                width="50" height="27" clipAndEnableScrolling="true">
<fx:Metadata>
    [Event(name="avatarSelected", type="events.AvatarSelected")]

</fx:Metadata>

<fx:Script>
    <![CDATA[
        import entity.Avatar;           
        import events.AvatarSelected;
        import mx.controls.Alert;

        protected function cBox_clickHandler(event:MouseEvent):void
        {
            var eventAvatar:AvatarSelected=new AvatarSelected("avatarSelected",Avatar (data));
            dispatchEvent(eventAvatar);

        }

    ]]>
</fx:Script>

    <s:CheckBox id="cBox" horizontalCenter="0" click="cBox_clickHandler(event)"/>

</s:GridItemRenderer>

我的自定义事件“AvatarSelected.as”

package events
{
import entity.Avatar;
import flash.events.Event;
import mx.states.OverrideBase;


public class AvatarSelected extends Event
{
    public static const SELECTED_AVATAR:String = "avatarSelected";

    public var avatar:Avatar;

    public function AvatarSelected(type:String, avatar:Avatar)
    {
        super(type);
        this.avatar =avatar;
    }

}

}

我的 VO “Avatar.as”

package entity
{
//[Bindable]
//[RemoteClass(alias="entity.Avatar")]

[Bindable]
public class Avatar
{
    public var idAvatar:Number;
    public var pseudo:String;

   }
}

我选择了另一种捕获事件的方法,我创建了一个扩展 GridColumn 以接受事件作为参数的 SkinDataGrid 类,但它还不起作用!

主应用程序上的 DataGrid:

<s:DataGrid x="171" y="333" width="649" height="194" fontFamily="Times New Roman" id="avatarGridII"
            fontSize="15" requestedRowCount="4" textAlign="center" dataProvider="{listAvatar}">
    <s:columns>
        <s:ArrayList>
            <skin:SkinDataGrid dataField="isAvatar" headerText="Id" ></skin:SkinDataGrid>
            <skin:SkinDataGrid dataField="pseudo" headerText="Pseudo"></skin:SkinDataGrid>
            <skin:SkinDataGrid  headerText="Selectionner" width="100" itemRenderer="rendered.CheckRendered" id="avatarColumnII" avatarSelected="avatarSelectedHandler(event)"></skin:SkinDataGrid>


        </s:ArrayList>
    </s:columns>

</s:DataGrid>

SkinDataGrid.as

  package skin
{
import spark.components.DataGrid;
import spark.components.gridClasses.GridColumn;

[Event(name="avatarSelected", type="events.AvatarSelected")]

public class SkinDataGrid extends GridColumn
{
    public function SkinDataGrid()
    {
        super();
    }

   }
}

任何想法 ?先感谢您。

4

2 回答 2

0

您没有指定您有什么样的问题(错误类型),但是通过查看我发现可疑的代码,当您将函数 avatarSelectedHandler 分配为 AvatarSelected 类型的处理程序时,该函数作为 MouseEvent 类型的参数。

于 2013-11-01T12:03:35.777 回答
0

您还可以尝试在启用冒泡的情况下调度此事件(默认情况下,它在 Event 类构造函数中设置为 false)。

在您的情况下, AvatarSelected 构造函数可以更改为:

public function AvatarSelected(type:String, avatar:Avatar, bubbles:Boolean=false, cancelable:Boolean=false)
{
    super(type, bubbles, cancelable);
    this.avatar =avatar;
}

并且事件创建将是这样的(注意 AvatarSelected() 中的第三个参数):

var eventAvatar:AvatarSelected = new AvatarSelected("avatarSelected", Avatar (data), true);
于 2013-11-02T02:20:07.777 回答