教你7步实现flex自定义Event及参数传递
Flex应用开发过程中如需要灵活的在不同组件(如A与B,父与子)之间响应事件,传递参数等功能时就会使用自定义事件(Event)机制,下面通过一个事例分七步,通过自定义Event和EventDispatcher两种机制实现事件交互和参数传递; 事例描述: 有一个父亲“parentApp.mxml”有两个儿子“comBrotherA.mxml”和\新年 降至,两个儿子为表孝心分别给他们老爸存入(事件)一笔过节费(事件参数),并通知老爸我存钱进去了,老爸在收到两个儿子的钱后汇总后同时告诉(事件)两 个儿子钱我已收到总数(事件参数)是多少...
1、第一步:引入自定义注册事件参数传递扩展类(来自网络)
view plaincopy to clipboardprint?
1. package myeventhelper 2. {
3. //自定义注册事件参数传递扩展类 4. public class EventArgExtend 5. {
6. public function EventArgExtend() 7. { 8. }
9. public static function create(f:Function,...arg):Function //动态参数创建 10. {
11. var F:Boolean = false;
12. var _f:Function = function(e:*,..._arg) 13. {
14. _arg = arg; 15. if(!F) 16. {
17. F = true;
18. _arg.unshift(e); 19. }
20. f.apply(null,_arg); 21. };
22. return _f; 23. }
24. public static function toString():String 25. {
26. return \
27. } 28. } 29.}
package myeventhelper { //自定义注册事件参数传递扩展类 public class EventArgExtend { public function EventArgExtend() { } public static function create(f:Functio { var F:Boolean = false; var _f:Function = function(e:*,..._ { _arg = arg; if(!F) { F = true; _arg.unshift(e); 2、第二步:自定义事件触发类: view plaincopy to clipboardprint?
1. package myeventhelper 2. {
3. import flash.events.EventDispatcher; 4.
5. import mx.core.UIComponent; 6. //自定义事件触发类
7. public class MyEventDispatcher extends EventDispatcher 8. {
9. private static var _instance:MyEventDispatcher;
10. public static const EXEC_PARENT_METHOD:String=\ntMethod\执行Parent方法
11. public static function getInstance():MyEventDispatcher
12. {
13. if(_instance==null){
14. _instance=new MyEventDispatcher(); 15. }
16. return _instance; 17. }
18. public var Source:UIComponent; //事件源对象 19. public var Parsms:Object; //主要用于参数传递 20. } 21.}
package myeventhelper { import flash.events.EventDispatcher; import mx.core.UIComponent; //自定义事件触发类 public class MyEventDispatcher extends Even { private static var _instance:MyEventDis public static const EXEC_PARENT_METHOD: public static function getInstance():My { if(_instance==null){ _instance=new MyEventDispatcher() } return _instance; } public var Source:UIComponent; //事件源 3、第三步:用户自定义事件类
view plaincopy to clipboardprint?
1. package myeventhelper 2. {
3. import mx.events.FlexEvent; 4. //用户自定义事件类
5. public class MyExtendEvent extends FlexEvent 6. {
7. public static const EXEC_BROTHER_METHOD:String=\cBrotherMethod\执行兄弟方法 8.
9. public var param:Object;
10. public function MyExtendEvent(o:Object,type:String, bubbles:Boolean=false, cancelable:Boolean=false) 11. {
12. super(type, bubbles, cancelable);
13. this.param = o;//也可通过这样的方式传递参数 14. } 15. } 16. }
package myeventhelper { import mx.events.FlexEvent; //用户自定义事件类 public class MyExtendEvent extends FlexEven { public static const EXEC_BROTHER_METHOD public var param:Object; public function MyExtendEvent(o:Object, { super(type, bubbles, cancelable); this.param = o;//也可通过这样的方式 } } } 4、第四步:完成儿子A“comBrotherA.mxml”:
view plaincopy to clipboardprint?
1.
2.
28. 29. 30. 31. 35. \36. 37.
微信: QQ: