Flex动态加载swc和swf中的class
来源:互联网 发布:宝宝成长日记软件 编辑:程序博客网 时间:2024/05/19 18:00
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;
- }
- //加载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,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);
- 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.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;
- }
- }
- //加载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.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);
- }
- }
- }
- Flex动态加载swc和swf中的class
- Flex动态加载swc和swf中的class
- Flex动态加载swc和swf中的class
- SWC和SWF文件比较
- SWC和SWF文件比较 .
- 动态加载SWC文件
- flex -------swc文件无法加载
- Flex SWFLoader加载SWF和元件,控制SWF
- Flex中实时动态加载(图像swf)皮肤的方法
- 查看flex中的swc包源码文件
- flex swfobject 加载远程swf
- 对于swc文件的动态加载
- flex加载外部的swf,取得swf内容的实际宽度和高度
- 使用Flex mxmlc编译命令选项,减小swc或swf的大小
- Study Flex《-swf-version编译器参数设置与playerglobal.swc的关系》
- ActionScript,Flash,Flash/Flex Builder,FlashPlayer,AIR,swf,swc,swz之间的区别
- 加载swf并获取swf中的元件
- Android中的class动态加载机制
- 上海大学生手机应用创意、开发大赛
- 世界上最有名的17句话
- window7 系统安装VS2010失败解决办法
- 安装 Qt Addin 后,VS2008 无法启动解决方法
- FZIP导入方法
- Flex动态加载swc和swf中的class
- 明确项目需求
- 我的2011——再见
- framebuffer驱动全篇(三)
- framebuffer驱动全篇(四)
- APK安装过程及原理详解
- 使用Executors
- InstallShield内部库函数全集(中文版)
- Libpcap库主要函数