`
liq39liq
  • 浏览: 14789 次
最近访客 更多访客>>
社区版块
存档分类
最新评论

Flex动态加载swc和swf中的class

 
阅读更多

Flex动态加载swc和swf中的class
2011年05月12日
  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:Functi on=null) { super(); if(value!=null){ this.swcUrl=value; } if(completeMethod!=null){ this.method=completeMethod; } this.visible=false; } //加载swc public 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,erro r); 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.CO MPLETE,libReady); loader.contentLoaderInfo.addEventListener(IOErrorE vent.IO_ERROR,swfError); loader.loadBytes(byte,new LoaderContext(false,ApplicationDomain.currentDomai n)); } 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); else zipEntry = 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.fil e::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:Functi on=null) { super(); if(value!=null){ this.swfUrl=value; } if(completeMethod!=null){ this.method=completeMethod; } } //加载swf public 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.CO MPLETE ,swfLoaded); loader.load(new URLRequest(this.swfUrl)); }else{ Alert.show("不可以加载空的swc地址"); } } //加载swc完成 private function swfLoaded(e:Event):void { if(method!=null) method.call(this); } } }
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics