从自定义组件分派自定义事件

发布于 2024-12-03 12:03:09 字数 4641 浏览 0 评论 0原文

我有一个自定义 Flex 4.5 组件(在列表中使用) -

Game

Game.mxml (代表纸牌游戏中的可点击游戏桌):

<?xml version="1.0" encoding="utf-8"?>
<s:ItemRenderer 
    xmlns:fx="http://ns.adobe.com/mxml/2009" 
    xmlns:s="library://ns.adobe.com/flex/spark" 
    xmlns:mx="library://ns.adobe.com/flex/mx" 
    xmlns:comps="*"
    width="160" height="160" 
    autoDrawBackground="false"
    creationComplete="init(event)">

    <fx:Metadata> 
        [Event(name="pref_event", type="PrefEvent")] 
    </fx:Metadata>

    <fx:Script>
        <![CDATA[
            import mx.events.FlexEvent;

            private var _data:XML;

            [Bindable("dataChange")]
            public override function get data():Object {
                return _data;
            }

            public override function set data(value:Object):void {
                _data = value as XML;
                if (null == _data)
                    return;

                gameid = _data.@id;

                for (var i:uint = 0; i < 3; i++) {
                    this['_user' + i].data = _data.elements('user')[i];
                }

                dispatchEvent(new FlexEvent(FlexEvent.DATA_CHANGE));
            }

            public function init(event:FlexEvent):void {
                _rect.filters = Util.SHADOW;
                addEventListener(MouseEvent.CLICK, handleClick);
            }

            private function handleClick(event:MouseEvent):void {
            trace("Clicked: " + gameid);
                dispatchEvent(new PrefEvent(PrefEvent.GAME_CLICKED, gameid, true));
            }

            public function set gameid(str:String):void {
                _gameid.text = '#' + str;
            }

            public function get gameid():String {
                return _gameid.text.substring(1);
            }
        ]]>
    </fx:Script>

    <s:Rect id="_rect" x="20" y="20" width="120" height="120" radiusX="16" radiusY="16">
        <s:stroke>
            <s:SolidColorStroke color="#4F69B5" weight="4"/>
        </s:stroke>     
        <s:fill>
            <s:SolidColor color="#CCDDEE" />
        </s:fill>
    </s:Rect>

    <s:Label id="_gameid" x="0" y="75" width="{width}" fontSize="16" textAlign="center" color="#FFFFFF" />

    <comps:User id="_user0" x="56"  y="4" scaleX=".3" scaleY=".3" interactive="false" visible="false" />
    <comps:User id="_user1" x="108" y="60" scaleX=".3" scaleY=".3" interactive="false" visible="false" />
    <comps:User id="_user2" x="4"   y="60" scaleX=".3" scaleY=".3" interactive="false" visible="false" />

</s:ItemRenderer>

我正在尝试从中分派自定义事件 - PrefEvent.as

package {
    import flash.events.Event;

    public class PrefEvent extends Event {
        public var str:String;
        public static const GAME_CLICKED:String = 'game_clicked';
        public static const CARD_CLICKED:String = 'card_clicked';
        public static const CARD_PLAYED:String  = 'card_played';

        public function PrefEvent(type:String, n:String, bubbles:Boolean = false, cancelable:Boolean = false){
            super(type, bubbles,cancelable);
            str = n;
        }

        public override function clone():Event {
            return new PrefEvent(type, str, bubbles, cancelable);
        }

        public override function toString():String {
            return str;
        }
    }
}

最后是我的测试代码 - GameTest.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"
    xmlns:comps="*"
    width="700" height="525" 
    creationComplete="init()">

    <fx:Script>
        <![CDATA[
            public function init():void {
                game.data = <game id="8946">
                                <user id="OK353118989212"/>
                                <user id="OK351923295875"/>
                            </game>;
            }       

            private function gameClicked(event:PrefEvent):void {
                trace("game clicked: " + event);
            }           
        ]]>
    </fx:Script>

    <comps:Game id="game" x="0" y="0" pref_event="gameClicked(event)" />

</s:Application>

但我从未看到“游戏点击:”跟踪。请问有人知道为什么吗?

我可能错过了一些小事情,因为我可以看到“Clicked: 8946”跟踪。

I have a custom Flex 4.5 component (to be used in a List) -

Game

Game.mxml (represents a clickable playing table in a card game):

<?xml version="1.0" encoding="utf-8"?>
<s:ItemRenderer 
    xmlns:fx="http://ns.adobe.com/mxml/2009" 
    xmlns:s="library://ns.adobe.com/flex/spark" 
    xmlns:mx="library://ns.adobe.com/flex/mx" 
    xmlns:comps="*"
    width="160" height="160" 
    autoDrawBackground="false"
    creationComplete="init(event)">

    <fx:Metadata> 
        [Event(name="pref_event", type="PrefEvent")] 
    </fx:Metadata>

    <fx:Script>
        <![CDATA[
            import mx.events.FlexEvent;

            private var _data:XML;

            [Bindable("dataChange")]
            public override function get data():Object {
                return _data;
            }

            public override function set data(value:Object):void {
                _data = value as XML;
                if (null == _data)
                    return;

                gameid = _data.@id;

                for (var i:uint = 0; i < 3; i++) {
                    this['_user' + i].data = _data.elements('user')[i];
                }

                dispatchEvent(new FlexEvent(FlexEvent.DATA_CHANGE));
            }

            public function init(event:FlexEvent):void {
                _rect.filters = Util.SHADOW;
                addEventListener(MouseEvent.CLICK, handleClick);
            }

            private function handleClick(event:MouseEvent):void {
            trace("Clicked: " + gameid);
                dispatchEvent(new PrefEvent(PrefEvent.GAME_CLICKED, gameid, true));
            }

            public function set gameid(str:String):void {
                _gameid.text = '#' + str;
            }

            public function get gameid():String {
                return _gameid.text.substring(1);
            }
        ]]>
    </fx:Script>

    <s:Rect id="_rect" x="20" y="20" width="120" height="120" radiusX="16" radiusY="16">
        <s:stroke>
            <s:SolidColorStroke color="#4F69B5" weight="4"/>
        </s:stroke>     
        <s:fill>
            <s:SolidColor color="#CCDDEE" />
        </s:fill>
    </s:Rect>

    <s:Label id="_gameid" x="0" y="75" width="{width}" fontSize="16" textAlign="center" color="#FFFFFF" />

    <comps:User id="_user0" x="56"  y="4" scaleX=".3" scaleY=".3" interactive="false" visible="false" />
    <comps:User id="_user1" x="108" y="60" scaleX=".3" scaleY=".3" interactive="false" visible="false" />
    <comps:User id="_user2" x="4"   y="60" scaleX=".3" scaleY=".3" interactive="false" visible="false" />

</s:ItemRenderer>

and I'm trying to dispatch a custom event from it - PrefEvent.as:

package {
    import flash.events.Event;

    public class PrefEvent extends Event {
        public var str:String;
        public static const GAME_CLICKED:String = 'game_clicked';
        public static const CARD_CLICKED:String = 'card_clicked';
        public static const CARD_PLAYED:String  = 'card_played';

        public function PrefEvent(type:String, n:String, bubbles:Boolean = false, cancelable:Boolean = false){
            super(type, bubbles,cancelable);
            str = n;
        }

        public override function clone():Event {
            return new PrefEvent(type, str, bubbles, cancelable);
        }

        public override function toString():String {
            return str;
        }
    }
}

And here is finally my test code - GameTest.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"
    xmlns:comps="*"
    width="700" height="525" 
    creationComplete="init()">

    <fx:Script>
        <![CDATA[
            public function init():void {
                game.data = <game id="8946">
                                <user id="OK353118989212"/>
                                <user id="OK351923295875"/>
                            </game>;
            }       

            private function gameClicked(event:PrefEvent):void {
                trace("game clicked: " + event);
            }           
        ]]>
    </fx:Script>

    <comps:Game id="game" x="0" y="0" pref_event="gameClicked(event)" />

</s:Application>

But I never see the "game clicked: " trace. Does anybody please know why?

I'm probably missing something minor, because I can see the "Clicked: 8946" trace.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

原来分手还会想你 2024-12-10 12:03:09

问题:

Game.mxml 文件中具有 "pref_event" 事件

[Event(name="pref_event", type="PrefEvent")]

,但您正在调度 PrefEvent.GAME_CLICKED ("game_clicked")

解决方案:

您必须为此调度正确的事件。

dispatchEvent( new PrefEvent("pref_event",gameid, true) );  

Problem:

In Game.mxml File has "pref_event" Event

[Event(name="pref_event", type="PrefEvent")]

but your dispatching PrefEvent.GAME_CLICKED ("game_clicked").

Solution:

You must dispatch the right event for this.

dispatchEvent( new PrefEvent("pref_event",gameid, true) );  
霊感 2024-12-10 12:03:09

[Event(name="pref_event", type="PrefEvent")] != new PrefEvent(PrefEvent.GAME_CLICKED, gameid, true)

事件名称不同。

[Event(name="pref_event", type="PrefEvent")] != new PrefEvent(PrefEvent.GAME_CLICKED, gameid, true)

The name of the events are not the same.

圈圈圆圆圈圈 2024-12-10 12:03:09

您应该在 Game.mxml 文件中写入以下内容:

<fx:Metadata> 
        [Event(name="game_clicked", type="PrefEvent")] 
</fx:Metadata>

在主文件中写入以下内容:

<comps:Game id="game" x="0" y="0" game_clicked="gameClicked(event)" />

请参阅 此处了解更多信息...

You should write this in your Game.mxml file :

<fx:Metadata> 
        [Event(name="game_clicked", type="PrefEvent")] 
</fx:Metadata>

And this in your main file :

<comps:Game id="game" x="0" y="0" game_clicked="gameClicked(event)" />

See here for further informations...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文