实现功能:定义自定义组件中<mx:Image>控件的鼠标经过事件:
自定义组件(名称为Component1.mxml)主要代码:
<fx:Script> private function mouseover(event:MouseEvent):void { //分发(广播)事件;当点击登录按钮时就触发了这一事件 var e:MyEvent= new MyEvent(MyEvent.myMOUSEOVER); this.dispatchEvent(e); } private function mouseout(event:MouseEvent):void { //分发(广播)事件;当点击登录按钮时就触发了这一事件 this.dispatchEvent(new MyEvent(MyEvent.myMOUSEOUT)); } ]]> </fx:Script> <fx:Metadata> <!--add event--> [Event(name="myMouseover",type="bridge.MyEvent")] [Event(name="myMouseout",type="bridge.MyEvent")] </fx:Metadata> <mx:Image id="imgShouZhan" mouseOver="mouseover(event)" mouseOut="mouseout(event)" height="10" width="10" right="2" bottom="2" scaleContent="true" name="imgShouZhan"/>
添加类:
package bridge { import flash.events.Event; public class MyEvent extends FlexEvent { public static const myMOUSEOVER:String="myMouseover"; public static const myMOUSEOUT:String="myMouseout"; public function MyEvent(type:String) { super(type, false, false); } } }
定义监听事件:
var compTemp:Component1= new Component1(); compTemp.GetImgShouZhan().addEventListener(MyEvent.myMOUSEOVER, SetImgShouZhanUrl);//GetImgShouZhan()方法返回<mx:Image>对象 private function SetImgShouZhanUrl(event:MyEvent):void { Alert.show("Preview"); }
上面的代码仍然无法监听到image控件的鼠标经过事件,请大神帮忙看看?
如果我在mxml中直接引用自定义控件,并定义监听事件是可以的,如下:
<cus:Component1 id="myComponent" x="10" y="37" myMouseover="SetImgShouZhanUrl(event)"></cus:Component1>
但是为什么我在AS中使用 var compTemp:Component1= new Component1();动态生成一个自定义组件时,添加监听事件就不行了呢?