Flex动态加载swc和swf中的class

来源:互联网 发布:宝宝成长日记软件 编辑:程序博客网 时间:2024/05/19 18:00
   flex中比较少人使用相关的反射,主要原因是因为avm编译模式,没办法做到java般的灵活的反射,那么就比较容易失去工厂模式的灵活的特性,但是flex也有相关反射,主要是用户swc和swf的反射,可以直接在内部提取class使用。

      1、元数据捆绑问题

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

   

view plaincopy to clipboardprint?
·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
  1. [Embed(source="resource/image/config/canvasLoading.gif" , mimeType="application/octet-stream")]  
  2.         public var _loadingGif:Class;  

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

   2、动态加载模块的问题

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

   3、Licence的动态加载

   这个是我最常用的地方。

 

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

     动态加载swc

   

view plaincopy to clipboardprint?
·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
  1. package com.shine.framework.Swc  
  2. {  
  3.     import flash.display.Loader;  
  4.     import flash.events.Event;  
  5.     import flash.events.IOErrorEvent;  
  6.     import flash.net.URLLoader;  
  7.     import flash.net.URLLoaderDataFormat;  
  8.     import flash.net.URLRequest;  
  9.     import flash.system.ApplicationDomain;  
  10.     import flash.system.LoaderContext;  
  11.     import flash.utils.ByteArray;  
  12.       
  13.     import mx.controls.Alert;  
  14.     import mx.core.UIComponent;  
  15.       
  16.     import nochump.util.zip.ZipEntry;  
  17.     import nochump.util.zip.ZipFile;  
  18.       
  19.     public class SwcManager extends UIComponent  
  20.     {  
  21.         //swc的路径   
  22.         public var swcUrl:String="";  
  23.         //library swf路径   
  24.         public var libraryUrl:String="";  
  25.         //加载完成的方法   
  26.         public var method:Function;  
  27.           
  28.           
  29.         public function SwcManager(value:String=null,completeMethod:Function=null)  
  30.         {  
  31.             super();  
  32.             if(value!=null){  
  33.                 this.swcUrl=value;  
  34.             }  
  35.               
  36.             if(completeMethod!=null){  
  37.                 this.method=completeMethod;  
  38.             }  
  39.             this.visible=false;  
  40.         }  
  41.           
  42.         //加载swc   
  43.         public function loadSwc(value:String=null,completeMethod:Function=null):void{  
  44.             if(value!=null){  
  45.                 this.swcUrl=value;  
  46.             }  
  47.               
  48.             if(completeMethod!=null){  
  49.                 this.method=completeMethod;  
  50.             }  
  51.             if(this.swcUrl!=null){  
  52.                 var loader:URLLoader = new URLLoader();  
  53.                 loader.addEventListener(Event.COMPLETE,swcLoaded);  
  54.                 loader.addEventListener(IOErrorEvent.IO_ERROR,error);  
  55.                 loader.dataFormat = URLLoaderDataFormat.BINARY;  
  56.                 loader.load(new URLRequest(swcUrl));  
  57.             }else{  
  58.                 Alert.show("不可以加载空的swc地址");  
  59.             }  
  60.         }  
  61.           
  62.           
  63.         //加载swc完成   
  64.         private function swcLoaded(e:Event):void  
  65.         {  
  66.             var byte:ByteArray = e.target.data;  
  67.             byte = swc2swf(byte);  
  68.             var loader:Loader = new Loader();  
  69.             loader.contentLoaderInfo.addEventListener(Event.COMPLETE,libReady);  
  70.             loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,swfError);  
  71.               
  72.             loader.loadBytes(byte,new LoaderContext(false,ApplicationDomain.currentDomain));  
  73.         }  
  74.           
  75.         private function error(e:Event):void{  
  76.             Alert.show("加载"+this.swcUrl+"失败");  
  77.         }  
  78.           
  79.         private function swfError(e:Event):void{  
  80.             Alert.show("加载"+this.swcUrl+"swf失败");  
  81.         }  
  82.           
  83.           
  84.         //加载library 完成    
  85.         private function libReady(e:Event):void  
  86.         {  
  87.             if(method!=null)  
  88.                 method.call(this);  
  89.         }  
  90.           
  91.         //解压   
  92.         public function swc2swf(byte:ByteArray):ByteArray  
  93.         {  
  94.             var zipFile:ZipFile = new ZipFile(byte);  
  95.             var zipEntry:ZipEntry = null;  
  96.             if(libraryUrl!=null&&libraryUrl.length!=0)  
  97.                 zipEntry = zipFile.getEntry(libraryUrl);  
  98.             else  
  99.                 zipEntry = zipFile.getEntry("library.swf");  
  100.             return zipFile.getInput(zipEntry);  
  101.         }  
  102.           
  103.     }  
  104. }  
                    

   使用教程

   首先动态加载swc

  

view plaincopy to clipboardprint?
·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
  1. var swcManage:SwcManager =new SwcManager;  
  2.             swcManage.loadSwc("framework.swc",loadComplete);  

  其次实例化object

 

view plaincopy to clipboardprint?
·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
  1. private function loadComplete():void{  
  2. var o:Object=ReferenceUtil.referenceClass("Licence.file::LicenceFile");  
  3. }  

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

 

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

 

view plaincopy to clipboardprint?
·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
  1. package com.shine.framework.Swf  
  2. {  
  3.     import flash.display.Loader;  
  4.     import flash.events.Event;  
  5.     import flash.net.URLRequest;  
  6.       
  7.     import mx.controls.Alert;  
  8.     import mx.core.UIComponent;  
  9.       
  10.     public class SwfManager extends UIComponent  
  11.     {  
  12.         //swc的路径   
  13.         public var swfUrl:String="";  
  14.         //加载完成的方法   
  15.         public var method:Function;  
  16.           
  17.         public function SwfManager(value:String=null,completeMethod:Function=null)  
  18.         {  
  19.             super();  
  20.               
  21.             if(value!=null){  
  22.                 this.swfUrl=value;  
  23.             }  
  24.               
  25.             if(completeMethod!=null){  
  26.                 this.method=completeMethod;  
  27.             }  
  28.         }  
  29.           
  30.         //加载swf   
  31.         public function loadSwf(value:String=null,completeMethod:Function=null):void{  
  32.             if(value!=null){  
  33.                 this.swfUrl=value;  
  34.             }  
  35.               
  36.             if(completeMethod!=null){  
  37.                 this.method=completeMethod;  
  38.             }  
  39.               
  40.             if(this.swfUrl!=null){  
  41.                 var loader:Loader = new Loader();    
  42.                 loader.contentLoaderInfo.addEventListener(Event.COMPLETE ,swfLoaded);    
  43.                 loader.load(new URLRequest(this.swfUrl));  
  44.             }else{  
  45.                 Alert.show("不可以加载空的swc地址");  
  46.             }  
  47.         }  
  48.           
  49.         //加载swc完成   
  50.         private function swfLoaded(e:Event):void  
  51.         {  
  52.             if(method!=null)  
  53.                 method.call(this);  
  54.         }  
  55.     }  
  56. }