Flex4 事件机制3

前端开发 作者: 2024-08-25 16:15:03
     Flex中所有的组件都间接继承自EventDispatcher,通过查看Flex API,了解一下这个类中的方法。         如图所见,该类包含了派发事件、监听事件、移出事件等方法,那么通过这个类就可以实现本文开篇所提出的那种情况,分析一下,监听事件的对象和派发事件的对象必须是同一个对象,这样事件才能被捕获,所以我们需要写一个单例的类,并且组合EventDispatcher,可以满
     Flex中所有的组件都间接继承自EventDispatcher,通过查看Flex API,了解一下这个类中的方法。         如图所见,该类包含了派发事件、监听事件、移出事件等方法,那么通过这个类就可以实现本文开篇所提出的那种情况,分析一下,监听事件的对象和派发事件的对象必须是同一个对象,这样事件才能被捕获,所以我们需要写一个单例的类,并且组合EventDispatcher,可以满足需求。
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,priority:int=0,useWeakReference:Boolean=true):void
		{
			eventDispatcher.removeEventListener(type,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>
原创声明
本站部分文章基于互联网的整理,我们会把真正“有用/优质”的文章整理提供给各位开发者。本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
本文链接:http://www.jiecseo.com/news/show_68483.html
flex4 flex 4 事件 机制 3