Flex动态加载swc和swf中的class

来源:互联网 发布:james blunt 知乎 编辑:程序博客网 时间:2024/05/21 07:57

  flex中比较少人使用相关的反射,主要原因是因为avm编译模式,没办法做到java般的灵活的反射,那么就比较容易失去工厂模式的灵活的特性,但是flex也有相关反射,主要是用户swc和swf的反射,可以直接在内部提取class使用。

      1、元数据捆绑问题

     相信很多朋友也遇到相关问题

[Embed(source="resource/image/config/canvasLoading.gif" , mimeType="application/octet-stream")]public var _loadingGif:Class;

 如果一个项目中捆绑过多元数据就会造成swf体积过大,但是往往也要面对一个需求,就是无需修改主要的flex源代码就可以修改嵌入文件的需求,那么用swc嵌入文件,再用flex动态加载swc是最好的办法了。

   2、动态加载模块的问题

    某些项目在前端的flex有可能动态加载某些模块的源代码,这些可能动态加载的UI、utils或者一些skin,放在动态加载的swc中也是一个比较好的解决方案;

   3、Licence的动态加载

   这个是我最常用的地方。

 

     相关的用途就不描述那么多,如果有需要的朋友就可以找到优点了:

     动态加载swc

package com.shine.framework.Swc{import flash.display.Loader;import flash.events.Event;import flash.events.IOErrorEvent;import flash.net.URLLoader;import flash.net.URLLoaderDataFormat;import flash.net.URLRequest;import flash.system.ApplicationDomain;import flash.system.LoaderContext;import flash.utils.ByteArray;import mx.controls.Alert;import mx.core.UIComponent;import nochump.util.zip.ZipEntry;import nochump.util.zip.ZipFile;public class SwcManager extends UIComponent{//swc的路径public var swcUrl:String="";//library swf路径public var libraryUrl:String="";//加载完成的方法public var method:Function;public function SwcManager(value:String=null,completeMethod:Function=null){super();if(value!=null){this.swcUrl=value;}if(completeMethod!=null){this.method=completeMethod;}this.visible=false;}//加载swcpublic function loadSwc(value:String=null,completeMethod:Function=null):void{if(value!=null){this.swcUrl=value;}if(completeMethod!=null){this.method=completeMethod;}if(this.swcUrl!=null){var loader:URLLoader = new URLLoader();loader.addEventListener(Event.COMPLETE,swcLoaded);loader.addEventListener(IOErrorEvent.IO_ERROR,error);loader.dataFormat = URLLoaderDataFormat.BINARY;loader.load(new URLRequest(swcUrl));}else{Alert.show("不可以加载空的swc地址");}}//加载swc完成private function swcLoaded(e:Event):void{var byte:ByteArray = e.target.data;byte = swc2swf(byte);var loader:Loader = new Loader();loader.contentLoaderInfo.addEventListener(Event.COMPLETE,libReady);loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,swfError);loader.loadBytes(byte,new LoaderContext(false,ApplicationDomain.currentDomain));}private function error(e:Event):void{Alert.show("加载"+this.swcUrl+"失败");}private function swfError(e:Event):void{Alert.show("加载"+this.swcUrl+"swf失败");}//加载library 完成 private function libReady(e:Event):void{if(method!=null)method.call(this);}//解压public function swc2swf(byte:ByteArray):ByteArray{var zipFile:ZipFile = new ZipFile(byte);var zipEntry:ZipEntry = null;if(libraryUrl!=null&&libraryUrl.length!=0)zipEntry = zipFile.getEntry(libraryUrl);elsezipEntry = zipFile.getEntry("library.swf");return zipFile.getInput(zipEntry);}}}

使用教程

   首先动态加载swc

var swcManage:SwcManager =new SwcManager;swcManage.loadSwc("framework.swc",loadComplete);

 其次实例化object

private function loadComplete():void{var o:Object=ReferenceUtil.referenceClass("Licence.file::LicenceFile");}

 获取到swc中的实例就可以非常容易做相关的操作;

 

  另外附上动态加载swf源代码

package com.shine.framework.Swf{import flash.display.Loader;import flash.events.Event;import flash.net.URLRequest;import mx.controls.Alert;import mx.core.UIComponent;public class SwfManager extends UIComponent{//swc的路径public var swfUrl:String="";//加载完成的方法public var method:Function;public function SwfManager(value:String=null,completeMethod:Function=null){super();if(value!=null){this.swfUrl=value;}if(completeMethod!=null){this.method=completeMethod;}}//加载swfpublic function loadSwf(value:String=null,completeMethod:Function=null):void{if(value!=null){this.swfUrl=value;}if(completeMethod!=null){this.method=completeMethod;}if(this.swfUrl!=null){var loader:Loader = new Loader();  loader.contentLoaderInfo.addEventListener(Event.COMPLETE ,swfLoaded);  loader.load(new URLRequest(this.swfUrl));}else{Alert.show("不可以加载空的swc地址");}}//加载swc完成private function swfLoaded(e:Event):void{if(method!=null)method.call(this);}}}


原创粉丝点击