HOW TO ADD AN EVENT TO A CUSTOM MXML COMPONENT USING EVENT META TAG
Using events is fun in MXML components is fun and easily you can specify events using Event meta tag inside the component like this:
<mx:Metadata>
[Event(name="myevent", type="com.MyEvent")]
</mx:Metadata>
The following lines defines the myevent attribute of the component like this:
FramedImage component is a custom component which displays a framed image.
For this tutorial the following files where used:
- FramedImage.mxml – our component (extends Canvas)
- MyEvent.as – our custom event (extends Event)
- FramedImageApp.mxml – main application that uses FramedImage component
FramedImageApp.mxml is loading an image using FramedImage.mxml by specifying an URL. Our component is loading the image from that URL and displays it and when the loading is completed an event is dispatch to let us know that the image is fully loaded. The component can be extended to dispatch even more events like: progress, start loading, etc.
You can go straight to the code section to study each file, but before seeing the code animportant thing you must know in order to succeed and be able to catch the event outside the component.
The string specified in the name attribute of the event meta tag must be the same with the string specified in the new event dispatched and the type specified in the type attribute must match the type of the event object dispatched.
To be even more clear take a look at the next image.
MyEvent.MY_EVENT = “myevent” is defined in MyEvent.as class as a public static constant.
Now enjoy the code…
FramedImage.mxml – the component
MyEvent.as – custom event
FramedImageApp.mxml – main application
Now lets see the component in action (the event will be added to the events text area)…
Thanks for Visiting.................
Thanks for Visiting.................
No comments:
Post a Comment