使用接口调用多个动态Module的同一方法

来源:互联网 发布:室内设计师证书知乎 编辑:程序博客网 时间:2024/05/01 19:30

问题:

        主页面有一个ModuleLoader,通过以改变URL加载不同的Module。要在页面点击一button调用实例里面的方法。

解决:

        使所有Module实现一个定义了要调用方法的接口。

主页面代码:
<mx:Application
xmlns:mx="http://www.adobe.com/2006/mxml"
layout="vertical">
<mx:Script>
<![CDATA[
private function displayModule( moduleUrl:String ):void
{
 var url:String = moduleLoader.url;
 if( url == moduleUrl ) return;
 if( url != null)
  moduleLoader.unloadModule();
 moduleLoader.url = moduleUrl;
}
private function showHandler():void
{
 displayModule( "ContactList.swf");
}
private function enterHandler():void
{
 displayModule( "ContactEntry.swf");
}

private function test():void
{
 var myModule:Imodule = moduleLoader.child as Imodule;
 myModule.test();
}
]]>

 

</mx:Script>
<mx:Panel title="Contacts:" width="100%" height="100%" horizontalAlign="center" verticalAlign="middle">
<mx:ModuleLoader id="moduleLoader" width="100%" height="100%"  url="ContactList.swf"/>
<mx:HBox width="100%">
<mx:Button label="textModule" click="showHandler();" />
<mx:Button label="checkModule"
click="enterHandler();" />
 <mx:Button label="调用模块内部的方法"  click="test()"/>
</mx:HBox>
</mx:Panel>
</mx:Application>

 

模块ContactEntry:

<mx:Module
xmlns:mx="http://www.adobe.com/2006/mxml" width="100%" height="100%" implements="Imodule">


<mx:Script>
<![CDATA[
import mx.controls.Alert;
public function test():void
{
  lab.text=txt.text;
}
]]>
</mx:Script>

 <mx:TextInput id="txt" text="12312312"/>
 <mx:Label id="lab" text=""/>
 
 <mx:Button id="btn" label="Button" click="test()"/>
</mx:Module>

模块ContactList:

<mx:Module
xmlns:mx="http://www.adobe.com/2006/mxml"
width="100%" height="100%" implements="Imodule">
<mx:Script>

 <![CDATA[
import mx.controls.Alert;
public function test():void
{
   lab.text=rbg.selection.label;
}
 ]]>
</mx:Script>
 <mx:RadioButtonGroup  id="rbg"/>
 <mx:RadioButton label="Button 1" groupName="rbg"/>
 <mx:RadioButton label="Button 2" groupName="rbg"/>
 <mx:Label id="lab" text=""/>
 <mx:Button id="btn" label="Button" click="test()"/>
</mx:Module>

接口

package
{
 public interface Imodule
 {
  function test():void;
 }
}

原创粉丝点击