Flex4 事件机制3

     Flex中所有的组件都间接继承自EventDispatcher,通过查看Flex API,了解一下这个类中的方法。         如图所见,该类包含了派发事件、监听事件、移出事件等方法,那么通过这个类就可以实现本文开篇所提出的那种情况,分析一下,监听事件的对象和派发事件的对象必须是同一个对象,这样事件才能被捕获,所以我们需要写一个单例的类,并且组合EventDispatcher,可以满足需求。

      myevents/TestEventDispatcher.as只是测试用。

package events
{
	import events.MyEvent;	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;	
	import mx.core.Singleton;
	public class TestEventDispatcher
	{
		private static var _inst:MyEventDispatcher;
		private var eventDispatcher:IEventDispatcher;
		public function MyEventDispatcher(singleton:Singleton)
		{
			if(singleton == null)
			{
				throw new Error("Create MyEventDispatcher Error!");
			}
			eventDispatcher = new EventDispatcher();
		}
		public static function getInstance():MyEventDispatcher
		{
			if (!_inst)
			{
				_inst = new MyEventDispatcher(new Singleton);
			}
			return _inst;
		}
		public function addEventListener(type:String, listener:Function, useCapture:Boolean=false, pririty:int=0, useWeakReference:Boolean=true):void
		{
			eventDispatcher.addEventListener(type, listener, useCapture, pririty, useWeakReference);
		}
		public function removeEventListener(type:String, listener:Function, useCapture:Boolean=false, priority:int=0, useWeakReference:Boolean=true):void
		{
			eventDispatcher.removeEventListener(type, listener, useCapture);
		}
		public function dispatchEvent(event:MyEvent):Boolean
		{
			return eventDispatcher.dispatchEvent(event);
		}
		public function hasEventListener(type:String):Boolean
		{
			return eventDispatcher.hasEventListener(type);
		}
		public function willTrigger(type:String):Boolean
		{
			return eventDispatcher.willTrigger(type);
		}
	}
}
class Singleton {}

events/MyEvent.as,我们可以在该类中加入一个dispatch方法,创建完事件之后可以派发,代码如下:      
/**
 * 派发事件
 * @return 
 */
public function dispatch():Boolean
{
  return MyEventDispatcher.getInstance().dispatchEvent(this);
}

 这样,创建完自定义事件之后,就可以直接派发事件了,那么使用起来就很方便了,下面是具体使用代码,一个应用中有两个自定义组件,组件一中有个TextArea,组件二中有个按钮,点击按钮,将数据传递到TextArea中。

       EventTest.mxml
<?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:components="components.*">
	<s:layout>
		<s:VerticalLayout/>
	</s:layout>
	<components:component1 />
	<components:component2 />
</s:Application>

   components/component1.mxml
<?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"
		 creationComplete="creationCompleteHandler(event)">
	<fx:Script>
		<![CDATA[
			import events.MyEventDispatcher;
			import events.MyEvent;
			import mx.events.FlexEvent;			
			protected function creationCompleteHandler(event:FlexEvent):void
			{
				MyEventDispatcher.getInstance().addEventListener(MyEvent.SHOWINFO, showInfo);
			}			
			protected function showInfo(event:MyEvent):void
			{
				textArea.text = event.data;
			}			
		]]>
	</fx:Script>
	<s:TextArea id="textArea" />
</s:Group>

components/component2.mxml
<?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">	
	<fx:Script>
		<![CDATA[
			import events.MyEvent;
			import events.MyEventDispatcher;
			protected function buttonClickHandler(event:MouseEvent):void
			{
				var myEvent:MyEvent = new MyEvent(MyEvent.SHOWINFO);
				myEvent.data = "哈哈";
				myEvent.dispatch();
			}
		]]>
	</fx:Script>
	<s:Button label="显示内容" click="buttonClickHandler(event)"/>
</s:Group>




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值