`
luhantu
  • 浏览: 199829 次
  • 性别: 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都在以下的包中。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics