`
luhantu
  • 浏览: 202938 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Flex ApplicationDomain

    博客分类:
  • Flex
阅读更多

如果要说这个已经是一个非常长的篇幅,网上七零八落的介绍也非常多,我在也不会多说,如果想了解,可以参考LoaderContext 的applicationDomain的介绍,讲解的也蛮详细的。如果还是不太明白,在网上发现一篇讲的蛮不错的文章,大家可以看看:深入理解Flash的沙箱 – Application Domains。这里面讲的非常详细,希望对大家能有所帮助。

我在这里要做的是对以上这些理论的验证,把他们放到不同的applicationDomain下来测试。大家可以把代码拿下来改变一下看看有何效果。

<?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[
			import com.mode.Welcome;
			
			import mx.controls.SWFLoader;
			import mx.core.IVisualElement;
			import mx.core.UIComponent;
			import mx.events.FlexEvent;
			import mx.events.ModuleEvent;
			import mx.managers.ISystemManager;
			import mx.managers.SystemManager;
			import mx.modules.IModuleInfo;
			import mx.modules.ModuleLoader;
			import mx.modules.ModuleManager;
			private var test:TestMoudle;
			private var aaa:Welcome;
			protected function button1_clickHandler(event:MouseEvent):void
			{
				// 用Loader不能加载其他项目的Module。
				var ldr:Loader = new Loader();
				var req:URLRequest = new URLRequest("TestMoudle.swf");
				var appDomainC:ApplicationDomain = ApplicationDomain.currentDomain;
				var ldrContext:LoaderContext = new LoaderContext(false, appDomainC);
				ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
				ldr.load(req, ldrContext);
			}
			
			private function completeHandler(event:Event):void
			{
				var child:IVisualElement = ((event.target as LoaderInfo).content as IFlexModuleFactory).create () as IVisualElement;
				var message:String = (child as Object).welcome("kneny");
				trace(message); 
				//用loader加载本项目module,在loader complete事件中就可以拿到module所以在的applicationDomain。
				var bbb:* = event.target.applicationDomain;
				var myGreeter:Class = bbb.getDefinition("com.mode.Welcome") as Class; 
				var myGreeterObject:Object = new myGreeter();
				var message1:String = myGreeterObject.sayHi("kenny");
				trace(message1);
			}  
			private var _moduleInfo:IModuleInfo;// 用ModuleManager加载module,必须在外边定义这个IModuleInfo类,不然第一次加载不生效。
			protected function button2_clickHandler(event:MouseEvent):void
			{
				//用ModuleManager可以加载本项目的Module和其他项目的Module。
				_moduleInfo = ModuleManager.getModule("TestMoudle.swf");
				Security.allowDomain("*");					
				_moduleInfo.addEventListener(ModuleEvent.READY, onModuleReady);
				_moduleInfo.load(new ApplicationDomain(ApplicationDomain.currentDomain));
			}
				
			private function onModuleReady(event:ModuleEvent):void
			{
				var _moduleInfo:IModuleInfo = event.currentTarget as IModuleInfo;
				var child:IVisualElement = _moduleInfo.factory.create () as IVisualElement;	
				var message:String = (child as Object).welcome("kneny");
				trace(message); 
				//用ModuleManager来加载的module必须等到Module CreationComplete完成后才能再起loaderInfo中得到其applicationDomain。
				child.addEventListener(FlexEvent.CREATION_COMPLETE,onModuleCreatedHandler);
				this.addElement(child);
			}
			
			private function onModuleCreatedHandler(event:FlexEvent):void
			{
				 var bbb:* =  event.target.loaderInfo.applicationDomain;
				var myGreeter:Class = bbb.getDefinition("com.mode.Welcome") as Class; 
				var myGreeterObject:Object = new myGreeter();
				var message1:String = myGreeterObject.sayHi("kenny");
				trace(message1); 
			}
			
			
			protected function button3_clickHandler(event:MouseEvent):void
			{
				//用来加载application的swf。
				var ldr:SWFLoader = new SWFLoader();
				var appDomainC:ApplicationDomain = new ApplicationDomain();
				var ldrContext:LoaderContext = new LoaderContext(false, appDomainC);
				ldr.addEventListener(Event.COMPLETE, swfLoaderCompleteHandler);
				ldr.loaderContext = ldrContext;
				ldr.load("Demo2.swf");
			}
			
			private function swfLoaderCompleteHandler(event:Event):void
			{
				var _moduleInfo:SWFLoader = event.currentTarget as SWFLoader;
				//用SWFLoader加载的application swf需要在application 完全加载完成后才能得到其application和loaderInfo下的applicationDomain。
				_moduleInfo.content.addEventListener(FlexEvent.APPLICATION_COMPLETE,onCreatHandler);
				this.addElement(_moduleInfo);
			}
			
			private function onCreatHandler(event:Event):void
			{
				if(event.target.application)
				{
				var message:String =  event.target.application["welcome"]("Kenny!");
				trace("onCreatHandler " + message);
				var bbb:* = event.target.loaderInfo.applicationDomain;
				var myGreeter:Class = bbb.getDefinition("com.mode.Welcome") as Class; 
				var myGreeterObject:Object = new myGreeter();
				var message1:String = myGreeterObject.sayHi("kenny");
				trace("onCreatHandler " +message1);
				} 	
			}
			
			private function button4_clickHandler(event:MouseEvent):void
			{
				// 用来记载cs创建的swf。
				var ldr:SWFLoader = new SWFLoader();
				var appDomainC:ApplicationDomain = ApplicationDomain.currentDomain;
				var ldrContext:LoaderContext = new LoaderContext(false, appDomainC);
				ldr.addEventListener(Event.COMPLETE, cs4LoaderCompleteHandler);
				ldr.loaderContext = ldrContext;
				ldr.load("cs4.swf");
			}
			
			private function cs4LoaderCompleteHandler(event:Event):void
			{
				//用SWFLoader加载cs创建的swf,在loader complete事件中就可以拿到swf所以在的applicationDomain。
				var _moduleInfo:SWFLoader = event.currentTarget as SWFLoader;
				this.addElement(_moduleInfo);
				var message:String = (_moduleInfo.content as Object).welcome("kneny");
				trace(message);  
				var bbb:* = event.target.loaderInfo.applicationDomain;
				var myGreeter:Class = bbb.getDefinition("RectTest") as Class; 
				var myGreeterObject:Object = new myGreeter();
				var message1:String = myGreeterObject.sayHi("kenny");
				trace("onCreatHandler " +message1);
			}
			
			protected function button5_clickHandler(event:MouseEvent):void
			{
				// 用Loader加载application的swf。
				var ldr:Loader = new Loader();
				var req:URLRequest = new URLRequest("Demo2.swf");
				var appDomainC:ApplicationDomain = new ApplicationDomain();
				var ldrContext:LoaderContext = new LoaderContext(false, appDomainC);
				ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, appCompleteHandler);
				ldr.load(req,ldrContext);
				var ui:UIComponent = new UIComponent();
				ui.addChild(ldr);
				this.addElement(ui);
			}
			
			private function appCompleteHandler(event:Event):void
			{
				if(event.target)
				{
					//用Loader加载的application swf需要在application 完全加载完成后才能得到其application和loaderInfo下的applicationDomain。
					event.target.content.addEventListener(FlexEvent.APPLICATION_COMPLETE,appOnCreatHandler);
				} 	
			} 
			
			private function appOnCreatHandler(event:Event):void
			{
				if(event.target.application)
				{
					var message:String =  event.target.application["welcome"]("Kenny!");
					trace("onCreatHandler " + message);
					var bbb:* = event.target.loaderInfo.applicationDomain;
					var myGreeter:Class = bbb.getDefinition("com.mode.Welcome") as Class; 
					var myGreeterObject:Object = new myGreeter();
					var message1:String = myGreeterObject.sayHi("kenny");
					trace("onCreatHandler " +message1);
				} 	
			}
			
			protected function button6_clickHandler(event:MouseEvent):void
			{
				//用ModuleLoader可以加载本项目的Module和其他项目的Module。
				var ldr:ModuleLoader = new ModuleLoader();
				ldr.addEventListener(ModuleEvent.READY, moduleLoaderReadyHandler);
				ldr.applicationDomain = ApplicationDomain.currentDomain;
				ldr.loadModule("TestMoudle.swf");
			}
			
			private function moduleLoaderReadyHandler(event:Event):void
			{
				var child:IVisualElement = (event as Object).module.factory.create () as IVisualElement;	
				var message:String = (child as Object).welcome("kneny");
				trace(message); 
				//用ModuleLoader来加载的module必须等到Module CreationComplete完成后才能再起loaderInfo中得到其applicationDomain。
				child.addEventListener(FlexEvent.CREATION_COMPLETE,onModuleCreatedHandler);
				this.addElement(child);
			}
		]]>
	</fx:Script>
	<s:layout>
		<s:VerticalLayout/>
	</s:layout>
	<s:HGroup>
		<s:Button label="Load current Moudle" click="button1_clickHandler(event)"/>
		<s:Button label="ModuleManager Load" click="button2_clickHandler(event)"/>
		<s:Button label="SWFLoader Load" click="button3_clickHandler(event)"/>
		<s:Button label="Load cs4" click="button4_clickHandler(event)"/>
		<s:Button label="Loader Load APP" click="button5_clickHandler(event)"/>
		<s:Button label="ModuleLoader Load" click="button6_clickHandler(event)"/>
	</s:HGroup>
</s:Application>

 需要加载Module和swf都在以下的包中。

分享到:
评论

相关推荐

    webloigc 中设置 flex crossdomain.xml 文件

    在WebLogic管理控制台中,转到`Servers` &gt; `Server Name` &gt; `Default Web Module` &gt; `MIME Types`,然后添加新的MIME类型,如`application/xml`,扩展名设为`crossdomain.xml`。 4. 部署更新:最后,保存并激活这些...

    FLEX 系统管理器 SystemManager

    SystemManager不仅负责控制Flex应用的基本元素,如应用窗口、Application实例、弹出窗口和光标,还负责管理ApplicationDomain中的类加载和执行。下面将详细讨论SystemManager的主要功能和工作流程。 1. **应用窗口...

    flex事件运行流程

    SystemManager是Flex应用的核心管理者,它负责应用程序窗口、Application实例、弹出窗口、光标管理,以及ApplicationDomain中的类。SystemManager是FlashPlayer加载的第一个类,它存储了应用窗口尺寸和位置信息,...

    Flex 预加载动画demo

    Flex中的ApplicationDomain和LoaderInfo对象提供了相关的事件,如`init`, `complete`, `progress`等,通过监听这些事件,我们可以更新预加载器的状态。 3. **样式定制**:Flex支持MXML和CSS来定义界面元素的样式。...

    flex的离线API

    2. **离线事件处理**:Flex 提供了离线事件,如`application.application.applicationDomain.domainMemoryAvailable`和`flash.events.NetConnection.Connect.Closed`等,开发者可以通过监听这些事件来判断网络状态,...

    flex 动态载入外部图片

    context.applicationDomain = ApplicationDomain.currentDomain; loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageLoaded); loader.load(new URLRequest("http://example.com/image.jpg"), ...

    flex调用swf——swf可以转成具体的对象,而非MovieClip

    关键在于使用LoaderContext并指定ApplicationDomain,这允许我们控制加载的SWF如何与当前应用的命名空间交互。 以下是一个基本示例,展示如何加载SWF并将其转换为非MovieClip对象: ```actionscript import flash....

    Flex运行时加载皮肤

    context.applicationDomain = new ApplicationDomain(ApplicationDomain.currentDomain); loader.load(request, loaderContext); ``` - 在`completeHandler`中处理加载完成后的逻辑,比如解析并应用皮肤资源到...

    flex 模块化modules源码例子

    5. **模块间的通信**:Flex模块可以通过事件、回调函数、或使用Flex的ApplicationDomain和SharedObject机制来实现模块间的数据共享和通信。 6. **模块优化**:为了提高性能,应考虑模块的大小和依赖关系。尽量保持...

    Flex之模块化

    此外,使用`ApplicationDomain`可以隔离不同模块的类,防止命名冲突,同时降低内存消耗。 7. **模块缓存**:Flex模块支持缓存,这意味着一旦模块被下载,它会被存储在本地,下次加载时可以从缓存中快速获取,提升...

    flex3 接受外部参数

    在Flex应用内部,这些参数可以通过`flash.system.ApplicationDomain.currentDomain.getParameters()`访问。 2. **FlashVars**: `flashVars`是SWF对象在HTML中加载时接收参数的常见方法。例如: ```html ...

    flex试题打包面试题挺好的

    根据提供的文件信息,我们可以归纳出一系列与Flex相关的知识点,这些知识点涵盖了Flex开发中的基础概念、事件处理、数据类型转换、数组操作等多个方面。下面将详细解释这些知识点。 ### 1. Flex 默认背景颜色的修改...

    flex帮助文档4a

    2. **添加面板容器**:`&lt;mx:Panel title="MyApplication" width="200" height="300"&gt;`,面板容器是Flex布局的基础,用于容纳其他UI元素。 3. **添加Label控件**:`&lt;mx:Label text="Welcome to Flex!" ...

    FLEX动态加载Model

    - 数据加载完成后,通常会解析成对象树,然后通过`ApplicationDomain`或`Class`工厂方法实例化Model类。 2. **事件驱动编程** - 在FLEX中,数据加载是异步的,这意味着代码不会阻塞等待数据加载完成。当数据加载...

    FLEX常见问题总汇

    16. **跨域访问**:为了解决Flex的安全限制,你需要在服务器上放置一个`crossdomain.xml`文件,以允许Flex应用访问你的资源。 以上解答了Flex编程中的一些常见问题,希望能对你的开发工作有所帮助。在实际使用中,...

Global site tag (gtag.js) - Google Analytics