Hi, I'm able to send a custom event from a component to an
application, but how do I send a custom event from the application to
a component?

example code:
mycomp.mxml:

<?xml version="1.0" encoding="utf-8"?>
 <!--this is myapp.mxml-->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml";
layout="absolute"
        xmlns:comp="*" xmlns:events="flash.events.*"  creationComplete="init()">
<mx:Metadata>
[Event(name="appEvent", type="flash.events.Event")]             
</mx:Metadata>
<mx:Script>
<![CDATA[
        import mx.controls.Alert;
        
        private function init():void {
                                addEventListener("compEvent",compEventHandler);
                        }
                        
        private function compEventHandler(e:Event):void {
        Alert.show("event received from component");
                }
                        
        private function sendEvent(e:Event):void {
        var myappEvent:Event = new Event("appEvent", true,true);
                dispatchEvent(myappEvent);
                        }
                ]]>
        </mx:Script>
        <comp:mycomp />
        <mx:Button  label="send event to component" id="appbutton"
click="sendEvent(event)" />
</mx:Application>


component:
<?xml version="1.0" encoding="utf-8"?>
  <!--this is mycomp.mxml-->
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml"; width="400"
height="300" creationComplete="compInit()">
        
<mx:Metadata>
        [Event(name="compEvent", type="flash.events.Event")] 
</mx:Metadata>
        
<mx:Script >
        <![CDATA[
        import mx.controls.Alert;
        private function compInit():void {
                addEventListener("appEvent",appEventHandler);
                }
        private function appEventHandler(e:Event):void {
                Alert.show("event received from application");
                }
        private function sendEvent(e:Event):void {
        var mycompEvent:Event = new Event("compEvent", true,true);
                dispatchEvent(mycompEvent);
                }
                ]]>
        </mx:Script>
        <mx:Button id="mybutton" x="200" label="send event to application"
click="sendEvent(event)" />
</mx:Canvas>


thanks!

Reply via email to