Apache flex 从自定义组件分派自定义事件
我有一个定制的Flex 4.5组件(在列表中使用)- Game.mxml(表示纸牌游戏中可点击的游戏桌): 最后是我的测试代码-GameTest.mxml:Apache flex 从自定义组件分派自定义事件,apache-flex,events,flex4,components,flex4.5,Apache Flex,Events,Flex4,Components,Flex4.5,我有一个定制的Flex 4.5组件(在列表中使用)- Game.mxml(表示纸牌游戏中可点击的游戏桌): 最后是我的测试代码-GameTest.mxml: package { import flash.events.Event; public class PrefEvent extends Event { public var str:String; public static const GAME_CLICKED:String = 'gam
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;
}
}
}
<?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>
;
}
私有函数(事件:PrefEvent):无效{
跟踪(“点击游戏:+事件”);
}
]]>
但我从来没有看到“游戏点击:”的痕迹。有人知道为什么吗
我可能遗漏了一些次要内容,因为我可以看到“Clicked:8946”跟踪。[Event(name=“pref\u Event”,type=“PrefEvent”)!=新建PrefEvent(PrefEvent.GAME_单击,gameid,true)
事件的名称不同。[Event(name=“pref_Event”,type=“PrefEvent”)]!=新建PrefEvent(PrefEvent.GAME_单击,gameid,true)
事件名称不同。您应该在Game.mxml文件中写入以下内容:
<fx:Metadata>
[Event(name="game_clicked", type="PrefEvent")]
</fx:Metadata>
[事件(name=“game\u clicked”,type=“PrefEvent”)]
在您的主文件中:
<comps:Game id="game" x="0" y="0" game_clicked="gameClicked(event)" />
有关更多信息,请参阅…您应该在Game.mxml文件中写入以下内容:
<fx:Metadata>
[Event(name="game_clicked", type="PrefEvent")]
</fx:Metadata>
[事件(name=“game\u clicked”,type=“PrefEvent”)]
在您的主文件中:
<comps:Game id="game" x="0" y="0" game_clicked="gameClicked(event)" />
有关更多信息,请参阅…问题:
在Game.mxml文件中有“pref_事件”事件
[Event(name="pref_event", type="PrefEvent")]
但是您的调度PrefEvent.GAME\u单击(“GAME\u单击”)
解决方案:
您必须为此调度正确的事件
dispatchEvent( new PrefEvent("pref_event",gameid, true) );
问题:
在Game.mxml文件中有“pref_事件”事件
[Event(name="pref_event", type="PrefEvent")]
但是您的调度PrefEvent.GAME\u单击(“GAME\u单击”)
解决方案:
您必须为此调度正确的事件
dispatchEvent( new PrefEvent("pref_event",gameid, true) );
对不起,我不明白?Flash Builder似乎对pref_事件(在游戏组件的代码完成中提供)很满意,这是因为您已经指定了元数据,但您的偶数名称是“pref_事件”,而实际发送的事件使用的名称是“Game_clicked”。你看到问题了吗?对不起,我不明白?Flash Builder似乎对pref_事件(在游戏组件的代码完成中提供)很满意,这是因为您已经指定了元数据,但您的偶数名称是“pref_事件”,而实际发送的事件使用的名称是“Game_clicked”。你看到问题了吗?