您的位置:首页 > 其它

一个Flex事件的简单的例子

2010-08-14 09:31 453 查看
/*******************************************************************************************************************************/

//Flex事件的参考代码

//2010年8月13日

//资料来源:自己整理

/*******************************************************************************************************************************/

代码功能:这里主要是用代码展示一个可以传递参数的自定义事件的例子。

/**************************************************************************************************************/

>>>AppEvent.cs

package eventClass//eventClass 命名空间包

{

import flash.events.Event;

public class AppEvent extends Event //继承于Event类。

{

public static const SHOW_INFOWINDOW:String = "widgetShowInfo";

//

private var _data:Object; //事件中参数

//

private var _callback:Function;

//

public function AppEvent(type:String, data:Object = null, callback:Function = null)

{

super(type, false, false);

_data = data;

_callback = callback;

}

//--------------------------------------------------------------------------

//

// Properties, this is very import thing

//

//--------------------------------------------------------------------------

public function get data():Object

{

return _data;

}

public function set data(value:Object):void

{

_data = value;

}

public function get callback():Function

{

return _callback;

}

public function set callback(value:Function):void

{

_callback = value;

}

public override function clone():Event

{

return new AppEvent(this.type, this.data);

}

}

}

/**************************************************************************************************************/

// EventBus.cs

package eventClass

{

import flash.events.Event;

import flash.events.EventDispatcher;

public class EventBus extends EventDispatcher

{

/** Application event buss instance */

private static var _eventBus:EventBus;

/** Lock to enforce singleton */

private static var lock:Boolean = false;

/**

* Normally the EventBus is not instantiated via the <b>new</b> method directly.

* The constructor helps enforce only one EvenBus availiable for the application

* (singeton) so that it asures the communication only via a sigle event bus.

*/

public function EventBus()

{

if (!lock)

throw new Error("ContainerEventDispatcher can only be defined once!");

}

/**

* The factory method is used to create a instance of the EventBus. It returns

* the only instanace of EventBus and makes sure no another instance is created.

*/

public static function getInstance():EventBus

{

if (_eventBus == null)

{

lock = true;

_eventBus = new EventBus();

lock = false;

}

return _eventBus;

}

/**

* Basic dispatch function, dispatches simple named events. In the case

* that the event is only significant by the event token (type string),

* this new dispatch method simplify the code.

*/

public function dispatch(type:String):Boolean

{

return dispatchEvent(new Event(type));

}

}

}

/**************************************************************************************************************/

<?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">

<fx:Declarations>

<!--

这个页面的主要效果是,如何在自定义事件的处理过程中传递参数。

这个也是一个比较重要的例子

-->

</fx:Declarations>

<fx:Script>

<![CDATA[

import eventClass.AppEvent;

import mx.controls.Alert;

private function mm():void

{

var tt:eventClass.EventBus;

tt = eventClass.EventBus.getInstance();

var obj:Object = new Object();

obj.name="xingchen";

//添加事件侦听

tt.addEventListener(eventClass.AppEvent.SHOW_INFOWINDOW,changHander);

//分发事件,这个就相当于事件的触发。 事件的触发在某一种程度上也可以理解为事件的分发,主要是注意后面的obj参数,她就是在事件中的来进行传递的参数

tt.dispatchEvent(new AppEvent(AppEvent.SHOW_INFOWINDOW,obj));

}

private function changHander(evt:AppEvent):void

{

var obj:Object = evt.data; //这里主要是进行函数参数的转换。

mx.controls.Alert.show(obj.name);

}

]]>

</fx:Script>

<mx:VBox height="100%" verticalAlign="middle" backgroundColor="blue" backgroundAlpha="0.2" width="100%">

<mx:HBox horizontalAlign="center" width="100%">

<mx:Button label="myButton" click="mm()"/>

</mx:HBox>

</mx:VBox>

</s:Application>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: