4、Flex3自定义事件、事件冒泡及传值、组件间的函数回调及传值

阅读更多

为了演示事件冒泡,demo写的可能有点繁琐。大家凑合看看。

1.project结构

 

4、Flex3自定义事件、事件冒泡及传值、组件间的函数回调及传值_第1张图片

2、主文件



	
		
	
	
		
		
	

 3、WorkSpace.as

package com.future.visual
{
	import com.future.event.SelectSongEvent;
	
	import component.SongList;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.containers.Canvas;
	import mx.controls.Alert;
	import mx.controls.Button;
	import mx.managers.PopUpManager;
	public class WorkSpace extends Canvas
	{
		private var songs:XML = 
									
									
									
									
								; 
		public function WorkSpace()
		{
			var btn:Button = new Button();
			btn.id="song";
			btn.label = "Select Song";
			btn.x = 30;
			btn.y = 400;
			btn.addEventListener(MouseEvent.CLICK,changeHandler);
			this.addChild(btn);
		}
		private function changeHandler(event:Event):void{
			var sl:SongList = SongList(PopUpManager.createPopUp(this,SongList,true));
			sl.title = "SongList" ;
			sl.data = songs;
			sl.callbackFunction = changeSong;
           	PopUpManager.centerPopUp(sl);
		}
		
		private function changeSong(str:String):void{
			var e:SelectSongEvent = new SelectSongEvent(SelectSongEvent.SELECT_SONG,true);
			e.selectedSong = str;
			this.dispatchEvent(e);
		}
	}
}

 4、弹出窗口SongList.mxml



	
		
	
	
		
			
				
				
				
			
		
		
			
					
		
	

 5、自定义事件、这里的属性最好写成private类型,再加上set、get方法。我这里写的是public 的

package com.future.event
{
	import flash.events.Event;

	public class SelectSongEvent extends Event
	{
		public static const SELECT_SONG:String = "select song";
		public var selectedSong:String;
		
		public function SelectSongEvent(type:String, bubbles:Boolean=false, cancelable:Boolean=false)
		{
			super(type, bubbles, cancelable);
		}
		
	}
}
 
  • FlexTest.rar (492.5 KB)
  • 下载次数: 21

你可能感兴趣的:(自定义事件,事件冒泡,组件传值,事件传值)