加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 03:43:49 所属栏目:百科 来源:网络整理
导读:基于松耦合的概念 自定义事件类型将取到很重要的作用 ? 当您创建自己的自定义 Event 类时,必须覆盖继承的? Event.clone() ?方法,以复制自定义类的属性。如果您未设置在事件子类中添加的所有属性,则当侦听器处理重新分派的事件时,这些属性将不会有正确的

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

?

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

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

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

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

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

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

copy

    <s:Application?xmlns:fx="http://ns.adobe.com/mxml/2009"???
  1. ???????????????xmlns:s="library://ns.adobe.com/flex/spark"???
  2. ???????????????xmlns:mx="library://ns.adobe.com/flex/mx"?minWidth="955"?minHeight="600"?xmlns:components="com.demo.view.components.*">??
  3. protected?function?testforms1_CopyTextHandler(event:TestEvent):void??
  4. ????????????{??
  5. ????????????????this.t.text?=?event.message;??
  6. ????????????}??
  7. ??
  8. ????<fx:Declarations>??
  9. ????????<!--?将非可视元素(例如服务、值对象)放在此处?-->??
  10. ????</fx:Declarations>??
  11. ????<components:testForms?x="23"?y="28"?CopyText="testforms1_CopyTextHandler(event)">??
  12. ????</components:testForms>??
  13. ????<s:TextInput?x="440"?y="28"?width="227"?height="184"?id="t"/>??
  14. </s:Application>??

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读