Flex4自定义事件类型Event的相关应用

来源:互联网 发布:linux反向解析 编辑:程序博客网 时间:2024/05/16 15:12

基于松耦合的概念 自定义事件类型将取到很重要的作用

 

当您创建自己的自定义 Event 类时,必须覆盖继承的 Event.clone() 方法,以复制自定义类的属性。如果您未设置在事件子类中添加的所有属性,则当侦听器处理重新分派的事件时,这些属性将不会有正确的值。

 

自定义事件类 继承flash.events.Event类 下面看代码 其中 message 是自定义的属性, 下面要使用这个属性 来传递参数

 

[java] view plaincopy
  1. package com.demo.event  
  2. {  
  3.     import flash.events.Event;  
  4.       
  5.     public class TestEvent extends Event  
  6.     {  
  7.         public static const EVENT_CLICK:String = "copy_text";  
  8.           
  9.         public var message:String;  
  10.           
  11.         public function TestEvent(type:String, message:String)  
  12.         {  
  13.             super(type);  
  14.             this.message = message;  
  15.         }  
  16.           
  17.         override public function clone():Event{    
  18.               
  19.             return new TestEvent(type,message);    
  20.               
  21.         }    
  22.     }  
  23. }  

 

接下来建立一个控件 来指派这个事件

注册事件 CopyText

<fx:Metadata>
  [Event(name="CopyText",type="com.demo.event.TestEvent")]
 </fx:Metadata>
指派事件

protected function button1_clickHandler(event:MouseEvent):void
   {
    dispatchEvent(new TestEvent("CopyText",tempText.text));
   }

[c-sharp] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <s:Group xmlns:fx="http://ns.adobe.com/mxml/2009"   
  3.          xmlns:s="library://ns.adobe.com/flex/spark"   
  4.          xmlns:mx="library://ns.adobe.com/flex/mx" width="400" height="300">  
  5.     <s:layout>  
  6.         <s:BasicLayout/>  
  7.     </s:layout>  
  8.     <fx:Metadata>  
  9.         [Event(name="CopyText",type="com.demo.event.TestEvent")]  
  10.     </fx:Metadata>  
  11.   
  12.     <fx:Script>  
  13.         <!--[CDATA[  
  14.             import com.demo.event.TestEvent;  
  15.             protected function button1_clickHandler(event:MouseEvent):void  
  16.             {  
  17.                 dispatchEvent(new TestEvent("CopyText",tempText.text));  
  18.             }  
  19.         ]]-->  
  20.     </fx:Script>  
  21.   
  22.     <fx:Declarations>  
  23.         <!-- 将非可视元素(例如服务、值对象)放在此处 -->  
  24.     </fx:Declarations>  
  25.     <s:TextInput x="10" y="10" height="107" width="260" id="tempText"/>  
  26.     <s:Button x="14" y="124" label="Copy" click="button1_clickHandler(event)"/>  
  27. </s:Group>  

 

最后将这个控件放到主程序中, 并使用了这个自定义事件

[c-sharp] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"   
  3.                xmlns:s="library://ns.adobe.com/flex/spark"   
  4.                xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" xmlns:components="com.demo.view.components.*">  
  5.     <s:layout>  
  6.         <s:BasicLayout/>  
  7.     </s:layout>  
  8.     <fx:Script>  
  9.         <!--[CDATA[  
  10.             import com.demo.event.TestEvent;  
  11.   
  12.             protected function testforms1_CopyTextHandler(event:TestEvent):void  
  13.             {  
  14.                 this.t.text = event.message;  
  15.             }  
  16.   
  17.         ]]-->  
  18.     </fx:Script>  
  19.     <fx:Declarations>  
  20.         <!-- 将非可视元素(例如服务、值对象)放在此处 -->  
  21.     </fx:Declarations>  
  22.     <components:testForms x="23" y="28" CopyText="testforms1_CopyTextHandler(event)">  
  23.     </components:testForms>  
  24.     <s:TextInput x="440" y="28" width="227" height="184" id="t"/>  
  25. </s:Application>  

0 0