从自定义组件分派自定义事件
我有一个自定义 Flex 4.5 组件(在列表中使用) -
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.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题:
在 Game.mxml 文件中具有 "pref_event" 事件
,但您正在调度 PrefEvent.GAME_CLICKED ("game_clicked")。
解决方案:
您必须为此调度正确的事件。
Problem:
In Game.mxml File has "pref_event" Event
but your dispatching PrefEvent.GAME_CLICKED ("game_clicked").
Solution:
You must dispatch the right event for this.
[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.
您应该在 Game.mxml 文件中写入以下内容:
在主文件中写入以下内容:
请参阅 此处了解更多信息...
You should write this in your Game.mxml file :
And this in your main file :
See here for further informations...