加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

flex加载module并相互之间通信

发布时间:2020-12-15 04:19:59 所属栏目:百科 来源:网络整理
导读:1、module_1的代码: ?xml version="1.0" encoding="utf-8"?mx:Module xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" layout="absolute" width="400" height="300"fx:D

1、module_1的代码:

<?xml version="1.0" encoding="utf-8"?>
<mx:Module xmlns:fx="http://ns.adobe.com/mxml/2009" 
		   xmlns:s="library://ns.adobe.com/flex/spark" 
		   xmlns:mx="library://ns.adobe.com/flex/mx" layout="absolute" width="400" height="300">
	<fx:Declarations>
		<!-- 将非可视元素(例如服务、值对象)放在此处 -->
	</fx:Declarations>
	<fx:Script>
		<![CDATA[
			public function getData():String
			{
				return "Module数据";
			}
		]]>
	</fx:Script>
	<s:Label x="86" y="74" text="module1" width="242" height="87" fontSize="36" id="lbl1"/>
</mx:Module>

2、module_2的代码:

<?xml version="1.0" encoding="utf-8"?>
<mx:Module xmlns:fx="http://ns.adobe.com/mxml/2009" 
		   xmlns:s="library://ns.adobe.com/flex/spark" 
		   xmlns:mx="library://ns.adobe.com/flex/mx" layout="absolute" width="400" height="300">
	<fx:Declarations>
		<!-- 将非可视元素(例如服务、值对象)放在此处 -->
	</fx:Declarations>
	<fx:Script>
		<![CDATA[
			private function changeData():void
			{
//				label1.text=parentApplication.m1.child.getData();//module之间不能相互通信,只能通过主程序当中间
				label1.text=parentApplication.m1.child.lbl1.text;
			}
		]]>
	</fx:Script>
	<s:Button x="232" y="179" label="m2的按钮" width="112" height="47" click="changeData()"/>
	<s:Label x="18" y="228" text="m2标签调用m1的数据" width="332" height="65" fontSize="33" id="label1"/>
</mx:Module>

一、静态加载Module并相互通信

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
			   xmlns:s="library://ns.adobe.com/flex/spark" 
			   xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
	<fx:Script>
		<![CDATA[
			protected function button1_clickHandler(event:MouseEvent):void
			{
//				label1.text=(m1.child as module_1).getData();
//				label1.text=(m1.child as module_1).lbl1.text;
				var module:Object=m1.child as Object;
//				label1.text=module.getChildByName("lbl1").text;
				label1.text=module.lbl1.text;
			}


			protected function button2_clickHandler(event:MouseEvent):void
			{
				var module:Object=m1.child as Object;
				module.x=(Math.random()*20).toString();
				module.y=(Math.random()*20).toString();
				module.lbl1.text="哈哈";
			}

		]]>
	</fx:Script>
	<s:layout>
		<s:BasicLayout/>
	</s:layout>
	<fx:Declarations>
		<!-- 将非可视元素(例如服务、值对象)放在此处 -->
	</fx:Declarations>
	<mx:ModuleLoader id="m1" url="module_1.swf"></mx:ModuleLoader>
	<mx:ModuleLoader id="m2" url="module_2.swf"></mx:ModuleLoader>
	<s:Label x="567" y="484" text="调用m1的数据" width="269" height="53" fontSize="35" id="label1"/>
	<s:Button x="573" y="524" label="按钮" width="105" height="38" click="button1_clickHandler(event)"/>
	<s:Button x="67" y="540" label="按钮" width="181" height="58" click="button2_clickHandler(event)"/>
</s:Application>


二、动态加载模块

(1)ModuleLoader

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
			   xmlns:s="library://ns.adobe.com/flex/spark" 
			   xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
	<s:layout>
		<s:BasicLayout/>
	</s:layout>
	<fx:Script>
		<![CDATA[
			protected function btn1_clickHandler(event:MouseEvent):void
			{
//				module.loadModule("module_1.swf");
//				module.unloadModule();
				module.url="module_1.swf";
			}


			protected function btn0_clickHandler(event:MouseEvent):void
			{
				module.url="module_2.swf";
			}

		]]>
	</fx:Script>
	<fx:Declarations>
		<!-- 将非可视元素(例如服务、值对象)放在此处 -->
	</fx:Declarations>
	<s:Panel id="pnl" title="动态加载Module" left="10" top="10" bottom="50" right="10">
		<mx:ModuleLoader id="module"></mx:ModuleLoader>
	</s:Panel>   
	<s:Button click="btn1_clickHandler(event)" label="加载模块1" horizontalCenter="-102" bottom="10" id="btn1" width="148" height="32"></s:Button>  
	<s:Button click="btn0_clickHandler(event)" label="加载模块2" horizontalCenter="161" bottom="10" id="btn0" width="180" height="32"></s:Button>
</s:Application>

(2)IModuleInfo

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
			   xmlns:s="library://ns.adobe.com/flex/spark" 
			   xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
	<s:layout>
		<s:BasicLayout/>
	</s:layout>
	<fx:Script>
		<![CDATA[
			import mx.core.IVisualElement;
			import mx.events.ModuleEvent;
			import mx.modules.IModuleInfo;
			import mx.modules.ModuleManager;  
			
			private var module:IModuleInfo;  
			private var moduleInstance:Object;//用于保存加载后的实例引用  			
			protected function button1_clickHandler():void
			{
				module = ModuleManager.getModule("module_2.swf");  
				module.addEventListener(ModuleEvent.READY,onModuleReady);   
				module.load();    
			}
			private function onModuleReady(e:ModuleEvent):void  
			{                 
				moduleInstance  = e.module.factory.create();            
				this.pnl.addElement(moduleInstance as IVisualElement);   
				moduleInstance.label1.text = Math.random().toString();  
				moduleInstance.x = moduleInstance.y = 100;  				
			}   
			
		]]>
	</fx:Script>
	<fx:Declarations>
		<!-- 将非可视元素(例如服务、值对象)放在此处 -->
	</fx:Declarations>
	<s:Panel id="pnl" title="动态加载Module" left="10" top="10" bottom="50" right="10"></s:Panel>   
	<s:Button click="button1_clickHandler()" label="load" horizontalCenter="0" bottom="10" id="btn1">   	
	</s:Button>  
</s:Application>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读