Flex中自定义事件
<?xml version="1.0" encoding="utf-8"?> ???//应用程序启动时注册监听 ??]]> ? ? ? 另一种事件响应方式(即从一个组件中监听另一个组件的方法) <?xml version="1.0" encoding="utf-8"?> ---Mycanvas 组件 ? <?xml version="1.0" encoding="utf-8"?> ? ? 还有一点需要注意,也是自己在平时中总结出来的,就是,事件只能在父类中添加监听,在当前类或是子类中触发。不能与不相关(平级的组件)之间调用:如: ? ???? <?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" ?????????? ?? xmlns:local="*" creationComplete="application1_creationCompleteHandler(event)"> ??? <fx:Metadata> ?????? [Event(name="bookSelected",type="events.Event")] ??? </fx:Metadata> ??? ??? <fx:Script> ?????? <![CDATA[ ?????????? import events.BookEntryEvent; ?????????? ?????????? import mx.controls.Alert; ?????????? import mx.events.FlexEvent; ? ?????????? protected function button1_clickHandler(event:MouseEvent):void ?????????? { ????????????? this.dispatchEvent(new Event("bookSelected")); ?????????? } ? ?????????? protected function application1_creationCompleteHandler(event:FlexEvent):void ?????????? { ????????????? this.addEventListener("bookSelected",onBookSelectEventHandler); ?????????? } ?????????? ?????????? private function onBookSelectEventHandler(e:Event):void ?????????? { ????????????? Alert.show("自定义监听的事件"); ?????????? } ? ?????? ]]> ??? </fx:Script> ??? <s:Button x="127" y="134" label="触发事件" width="202" height="38" click="button1_clickHandler(event)"/> ??? <local:listenerTest/> </s:Application> ? ?
? <?xml version="1.0" encoding="utf-8"?> <s:Group 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="400" height="300" ?????? ?creationComplete="group1_creationCompleteHandler(event)"> ??? <s:layout> ?????? <s:BasicLayout/> ??? </s:layout> ??? <fx:Script> ?????? <![CDATA[ ?????????? import events.BookEntryEvent; ?????????? ?????????? import mx.controls.Alert; ?????????? import mx.events.FlexEvent; ? ??? ?????? protected function group1_creationCompleteHandler(event:FlexEvent):void ?????????? { ????????????? mybtn.addEventListener("bookSelected",onBookSelectEventHandler); ?????????? } ?????????? ?????????? private function onBookSelectEventHandler():void ?????????? { ????????????? Alert.show("自定义监听的事件"); ?????????? } ? ?????? ]]> ??? </fx:Script> ??? <fx:Declarations> ?????? <!-- 将非可视元素(例如服务、值对象)放在此处 --> ??? </fx:Declarations> ??? <s:Button label="45234drfsdfsdf" x="100" id="mybtn"/> </s:Group> ? 上面举得这个示例,就不能派发Group中的事件。??? 如果是使用父类与子类关系的事件监听的话,一定要把new Event()中的bubbles属性设置为true,表示该事件为冒泡事件。否则,事件不会派发。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |