Flex Module通信(2)——使用事件

      上一篇讨论通过实现接口来实现Application和Modules的通信,本篇讨论通过使用事件实现Application和Modules的通信。在一个swf中,组件间的交互通信可以直接使用addEventListener和dispatchEvent来完成事件的传递。当然这样的标准做法也适用于Module。

      通过addEventListener和dispatchEvent在Module发布事件:

 1:  <?xml version="1.0" encoding="utf-8"?>
 2:  <mx:Module xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="onComplete()">
 3:      <mx:Script>
 4:          <![CDATA[
 5:              import mx.events.DynamicEvent;
 6:   
 7:              [Bindable]private var command:String="";
 8:              private function onComplete():void
 9:              {
                     //从Application订阅
10:                  var sharedEventDispatcher:IEventDispatcher=systemManager.loaderInfo.sharedEvents;
11:   
12:                  sharedEventDispatcher.addEventListener("init",onInit,false,0,true);
13:                  //通知Application:Module创建完成
14:                  sharedEventDispatcher.dispatchEvent(new Event(Event.COMPLETE));
15:              }
16:    
17:              private function onInit(event:DynamicEvent):void
18:              {
19:                  command=event.data as String;
20:              }
21:   
22:          ]]>
23:      </mx:Script>
24:      
25:       <mx:Panel id="panel" title="Module With Events. {command}" width="400"  height="200"/>
26:  </mx:Module>
27:   

 

         Application的代码

 1:  <?xml version="1.0" encoding="utf-8"?>
 2:  <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
 3:      <mx:Script>
 4:          <![CDATA[
 5:              import mx.events.DynamicEvent;
 6:              import mx.controls.Alert;
 7:              import mx.events.ModuleEvent;
 8:              import mx.modules.Module;
 9:   
10:              private const MODULE_URL:String="ModuleWithEvents.swf";
11:              [Bindable] private var moduleLoaded:Boolean;
12:   
13:              private function loadModule():void {
14:                 //从Module订阅通知
15:                 var sharedEventDispatcher:IEventDispatcher = moduleLoader.loaderInfo.sharedEvents;
16:   
17:                 sharedEventDispatcher.addEventListener(Event.COMPLETE, onModuleCreated);
18:   
19:                 moduleLoader.loadModule(MODULE_URL);
20:   
21:                 moduleLoaded = true;
22:   
23:              }
24:   
25:              private function onModuleCreated(event:Event):void {
26:   
27:                  trace("Module CreateComplete happened");
28:   
29:                  //发送命令到Module
30:                  var sharedEventDispatcher:IEventDispatcher = moduleLoader.loaderInfo.sharedEvents;
31:   
32:                  var dynamicEvent:DynamicEvent = new DynamicEvent("init");
33:   
34:                  dynamicEvent.data = " Test Title!";
35:   
36:                  sharedEventDispatcher.dispatchEvent(dynamicEvent);
37:   
38:               }
39:   
40:               private function unloadModule():void {
41:                     moduleLoader.unloadModule();
42:                     moduleLoaded = false;
43:               }
44:          ]]>
45:   
46:     </mx:Script>
47:   
48:   <mx:HBox>
49:  
50:          <mx:Button label="Load Module" click="loadModule()" />
51:  
52:          <mx:Button label="Unload Module" click="unloadModule()"
53:   
54:          enabled="{moduleLoaded}"/>
55:  
56:          </mx:HBox>
57:  
58:  
59:  
60:   <mx:ModuleLoader id="moduleLoader" y="30"/>
61:  </mx:Application>
62:  

 

        运行结果,LoadModule后显示从Application传入的内容:

image

posted @ 2010-07-29 00:56  Asharp  阅读(2555)  评论(0编辑  收藏  举报