通过接口实现Application和Module通信

模块是独立,通过继承接口,允许外部应用程序与他们通信。

      首先,定义接口ICommunicaton.as:

package com.st.interfaces
{
	public interface ICommunication
	{
		function getMessage():String;
		
		function setMessage(value:String):void;
	}
}

创建Module继承ICommunicaton接口:

<?xml version="1.0" encoding="utf-8"?>
<mx:Module xmlns:mx="http://www.adobe.com/2006/mxml" implements="com.st.interfaces.ICommunication">
	<mx:Script>
		<![CDATA[
			import com.st.interfaces.ICommunication;
			
			[Bindable]private var _value:String = "";
			
			public function setMessage(value:String):void
			{
				_value = value;
			}
			
			public function getMessage():String
			{
				return _value;
			}
			
		]]>
	</mx:Script>
	<mx:Panel id="panel" title="Message:{_value}" width="400" height="200"/>
</mx:Module>

在Application通过ICommunication调用Module的方法(注意:不要用moduleLoader.child as (Module),这样就会把Module也编译进来):

var communication:ICommunication = moduleLoader.child as ICommunication;
				
				communication.setMessage("loaded by app");
Application的代码:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" minWidth="955" minHeight="600">
	<mx:Script>
		<![CDATA[
			import com.st.interfaces.ICommunication;
			
			import mx.containers.Panel;
			import mx.modules.Module;
			
			private const MODULE_URL:String="com/st/model/MyModule2.swf";
			[Bindable] private var moduleLoaded:Boolean;

			private function onModifyMessage():void
			{
				var communication:ICommunication = moduleLoader.child as ICommunication;
				
				communication.setMessage("loaded by app");
				
				var module:Module = moduleLoader.child as Module;
				
				var panel:Panel = module.getChildByName("panel") as Panel;
				
				trace(panel.title);
				
			}
							
		]]>
	</mx:Script>
	<mx:HBox>
		<mx:Button id="btnLoad" label="Load Module" click="moduleLoader.loadModule(MODULE_URL)"/>
		<mx:Button id="btnModify" label="Modify Model" click="onModifyMessage()"/>
		<mx:Button label="Unload Module" click="moduleLoader.unloadModule()"/>
	</mx:HBox>
	<mx:ModuleLoader applicationDomain="{ApplicationDomain.currentDomain}" id="moduleLoader" y="30"/>
</mx:Application>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值