Listen for custom events and handle instance code for events in as _flex

Source: Internet
Author: User
Tags assert xmlns
Scenario Description:Click on a picture to respond to the event. In as, you must listen to the event and handle the event.
1 Custom an event, as follows:
Copy Code code as follows:

Package Bridge {
public class MyEvent extends Event {
public static const myclick:string= "Myclick";
Public Function MyEvent (type:string, Bubbles:boolean=false, Cancelable:boolean=false)
Super (Type, bubbles, cancelable);

2 The As class that listens for event processing, as follows:
Copy Code code as follows:

Package Handler
Import Bridge. MyEvent;
Import Mx.controls.Alert;
Import skin. Imghanderskin;
Import Spark.components.BorderContainer;
Import Spark.components.Image;
public class Imghander extends Bordercontainer
[Skinpart (required= "true")]
public var img:image;//Open button
Public Function Imghander ()
Super ();
This.setstyle ("Skinclass", Imghanderskin);
Initializing listening
Override Public Function Initialize (): void{
Super.initialize ();
Img.addeventlistener (Myevent.myclick,setimgshouzhanurl);
Private Function Setimgshouzhanurl (event:event): void { ("Preview");

3 new Mxml skins, skin classes, where you send custom events. (There is a picture under the Assert folder: Conan. jpg) as follows:
Copy Code code as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
ADOBE SYSTEMS Incorporated
Copyright 2008 Adobe Systems Incorporated
All Rights Reserved.
Notice:adobe permits you, modify, and distribute this file
In accordance with the terms of the license agreement accompanying it.
<!---The default skin class for a Spark skinnablecontainer container.
@see Spark.components.SkinnableContainer
@langversion 3.0
@playerversion Flash 10
@playerversion AIR 1.5
@productversion Flex 4
<s:skin xmlns:fx= "" xmlns:s= "Library://"
xmlns:fb= "" alpha.disabled= "0.5" >
<fx:metadata>[hostcomponent ("Spark.components.BorderContainer")]</fx:metadata>
<fx:script fb:purpose= "Styling" >
<! [cdata[
Import Bridge. MyEvent;
* @private
Override protected function Updatedisplaylist (Unscaledwidth:number, unscaledheight:number): void
Push BackgroundColor and Backgroundalpha directly.
Handle undefined backgroundcolor by hiding the background object.
if (isNaN (GetStyle ("BackgroundColor"))
Background.visible = false;
Background.visible = true;
Bgfill.color = GetStyle ("BackgroundColor");
Bgfill.alpha = GetStyle ("Backgroundalpha");
Super.updatedisplaylist (Unscaledwidth, unscaledheight);
Private Function Img_mouseouthandler (event:mouseevent): void{
TODO auto-generated Method Stub
var e:myevent= new MyEvent (Myevent.myclick);
Img.dispatchevent (e);
<s:state name= "Normal"/>
<s:state name= "Disabled"/>
<!---defines the appearance of the Skinnablecontainer class ' s background. -->
<s:rect id= "Background" left= "0" right= "0" top= "0" bottom= "0" >
<s:solidcolor id= "Bgfill" color= "#FFFFFF"/>
Note:setting the minimum size to 0 this changes to the host component ' s
The size is not is thwarted by the skin part ' minimum size. This is a compromise,
More about it here:
<!---@copy Spark.components.skinnablecontainer#contentgroup-->
<s:group id= "Contentgroup" left= "0" right= "0" top= "0" bottom= "0" minwidth= "0" minheight= "0" >
<s:image id= "img" click= "Img_mouseouthandler (event)" Source= "assert/Conan. jpg" >

4 Finally, create a mxml application inside, to invoke the As class. Run directly.
Copy Code code as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
<s:application xmlns:fx= ""
Xmlns:s= "Library://"
xmlns:mx= "Library://"
Minwidth= "955" minheight= "600"
Xmlns:handler= "Handler.*" >

1 To send custom events:
Copy Code code as follows:

var e:myevent= new MyEvent (Myevent.myclick);
Img.dispatchevent (e);

2 Receive and Process events:
Img.addeventlistener (Myevent.myclick,setimgshouzhanurl);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.