Flex Modules通訊(1)——通過介面

來源:互聯網
上載者:User

     Flex Modules通訊方式有很多種:通過繼續介面、參數傳遞、定義事件通訊。這裡討論通過繼續介面通訊。模組是獨立,通過繼承介面,允許外部應用程式與他們通訊。

      首先,定義介面ICommunicaton.as:

1:  package
2:   {
3:      public interface ICommunication
4:      {
5:          function getMessage():String;
6:   
7:          function setMessage(value:String):void;
8:      }
9:  }

 

      建立Module繼承ICommunicaton介面:

 1:  <?xml version="1.0" encoding="utf-8"?>
 2:  <mx:Module xmlns:mx="http://www.adobe.com/2006/mxml" implements="ICommunication" >
 3:      <mx:Script>
 4:          <![CDATA[
 5:              [Bindable]private var _value:String="";
 6:   
 7:              public function setMessage(value:String):void
 8:              {
 9:                  _value=value;
10:              }
11:   
12:              public function getMessage():String
13:              {
14:                  return _value;
15:              }
16:          ]]>
17:      </mx:Script>
18:      
19:      <mx:Panel id="panel" title="Message :{_value}" width="400" height="200"/>
20:  </mx:Module>
21:  

 

      在Application通過ICommunication調用Module的方法:

1:  var communication:ICommunication=moduleLoader.child as ICommunication;
2:  communication.setMessage("loaded by application");

    

      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.containers.Panel;
 6:              import mx.modules.Module;
 7:   
 8:              private const MODULE_URL:String="Module1.swf";
 9:   
10:              private function onModifyMessage():void
11:              {
12:                  var communication:ICommunication=moduleLoader.child as ICommunication;
13:                  communication.setMessage("loaded by application");
14:   
15:                  var module:Module = moduleLoader.child as Module;
16:                  var panel:Panel=module.getChildByName("panel") as Panel;
17:                  trace(panel.title);
18:   
19:              }
20:          ]]>
21:      </mx:Script>
22:      
23:      <mx:HBox>
24:          <mx:Button id="btnLoad" label="Load Module" click="moduleLoader.loadModule(MODULE_URL)" />
25:          <mx:Button id="btnModify" label="Modify Module" click="onModifyMessage()"/>
26:          <mx:Button label="Unload Module" click="moduleLoader.unloadModule()" />
27:      </mx:HBox>
28:      <mx:ModuleLoader id="moduleLoader" y="30"/>
29:  </mx:Application>
30:  

 

 

 

 

        運行結果,載入後點擊Modify Module:

 

      注意:雖然使用繼承介面實現Application和Modules通訊很方便,實現介面就可以,但是在調用Modules方法是會鎖定Modules記憶體,使用時得注意。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.