近期要使用flex module机制对项目进行优化,但在刚划分模块后遇到一个诡异的问题,加载来的module怎么都无法显示到舞台上,百思不得其解,故晚上抽时间看了下module相关的源代码吗,终于发现问题所在。先直接说问题,再讲解一下module机制吧。
原因就出在ModuleInfoProxy里对info的几个事件进行了弱引用的侦听器,所以事件就没有在ModuleInfoProxy中捕获到,所以也就没有再传到我们的应用层了,现行处理办法:一定要做ModuleEvent.PROGRESS事件侦听,这样就可以保证ModuleInfoProxy中的moduleEventHandler侦听器函数一直没有失效。
---------------------------------------------------
那么在flex中是怎么处理Module的呢?
首先要熟悉FlexModuleFactory,它和SystemManager同继承自MovieClip,都是用来引导主文档类的,同实现IFlexModuleFactory接口,前面大概介绍过SystemManager类,这里再简单的说一下,它们都至少有两帧,第一帧作为loading处理,主类在第二帧上,在第一帧的时候会处理诸如显示进度条、加载RSL等工作,在第一阶段工作完成且主类已经加载完成的情况下开始对主类做一些初始化处理,如注册一些框架单例等。(另外这些类里有很多的小技巧可以在平时借鉴,如update()管理STATE的变化,而update再由特定事件驱动)
如果是Module,那么会在这一步向外抛出ready事件。
再看flex是如何用ModuleManager管理Module的,首先对module的加载是交给ModuleInfo来处理的,用ModuleInfo来实现Load()并做各种事件侦听,而ModuleInfo又被ModuleInfoProxy所封装,向外提供更加简明的接口,封装的一个工作就是对ModuleInfo的几个事件进行转发,如下图:
这就是上面说的弱侦听器导致的问题。
因为module变量是loadModule方法里面的临时变量,因此一旦调用方法执行结束则module的生存周期结束,因此module肯定被标识为垃圾回收对象。又由于其是以弱引用方式加入监听器,所以事件不会再派发到需要回收的监听器上。
解决方案:把module变量改变为类属性以延长了其生存周期
参考:http://hi.baidu.com/vim888/blog/item/1e9fcc3474cf99a1d1a2d35b.html
2011年4月26日星期二
2011年4月9日星期六
关于flex框架启动的一些事(1)
flex编译器在编译flex工程时会创建出四个基本类:
//
_MyProject_mx_managers_SystemManager extends mx.managers.SystemManager
SystemManager类为flash项目的文档类
这里flex编译器会为项目生成一个SystemManager的子类,在其中生成与项目相关的一些数据,如:
create()方法中定义该项目的主类名称;
info()方法中生成很多与项目相关的信息
//
MyProject extends spark.components.Application
Application为项目的主类
这里flex编译器会为项目生成一个Application的子类,在该类要完成做为容器的功能(mxmlcontent),还要完成整个项目的style功能:
在set moduleFactory(factory:IFlexModuleFactory)方法(被加入显示列表前调用)中调用一个生成的MyProject_StylesInit()方法,在该方法中完成自定义css文件中所有样式的注册,如:
selector = null;
conditions = null;
conditions = [];
condition = new CSSCondition("class", "Button1");
conditions.push(condition);
selector = new CSSSelector("spark.components.Button", conditions, selector);
// spark.components.Button.Button1
style = styleManager.getStyleDeclaration("spark.components.Button.Button1");
if (!style)
{
style = new CSSStyleDeclaration(selector, styleManager);
}
if (style.factory == null)
{
style.factory = function():void
{
this.backgroundImage = _embed_css____assets_ui_add_png_867674143;
};
}
//
_TestProject_FlexInit
[Mixin]类,被写入SystemManager::info()["minxins"]中,在SystemManager初始货完成加载Application之前(SystemManager::kickoff()中)调用静态的init方法
此类完成各种单例、效果、别名等的注册
//
_TestProject_Styles
[Mixin]类,被写入SystemManager::info()["minxins"]中,在SystemManager初始货完成加载Application之前(SystemManager::kickoff()中)调用静态的init方法
此类完成系统内置组件样式、皮肤的绑定,如:
selector = null;
conditions = null;
conditions = null;
selector = new CSSSelector("spark.components.Button", conditions, selector);
mergedStyle = styleManager.getMergedStyleDeclaration("spark.components.Button");
style = new CSSStyleDeclaration(selector, styleManager, mergedStyle == null);
if (style.defaultFactory == null)
{
style.defaultFactory = function():void
{
this.skinClass = spark.skins.spark.ButtonSkin;
};
}
if (mergedStyle != null &&
(mergedStyle.defaultFactory == null ||
ObjectUtil.compare(new style.defaultFactory(), new mergedStyle.defaultFactory())))
{
styleManager.setStyleDeclaration(style.mx_internal::selectorString, style, false);
}
///////////////////////////////////
根据这些信息,可以思考如何优化flex工程的编译速度,及缩减项目swf的大小
//
_MyProject_mx_managers_SystemManager extends mx.managers.SystemManager
SystemManager类为flash项目的文档类
这里flex编译器会为项目生成一个SystemManager的子类,在其中生成与项目相关的一些数据,如:
create()方法中定义该项目的主类名称;
info()方法中生成很多与项目相关的信息
//
MyProject extends spark.components.Application
Application为项目的主类
这里flex编译器会为项目生成一个Application的子类,在该类要完成做为容器的功能(mxmlcontent),还要完成整个项目的style功能:
在set moduleFactory(factory:IFlexModuleFactory)方法(被加入显示列表前调用)中调用一个生成的MyProject_StylesInit()方法,在该方法中完成自定义css文件中所有样式的注册,如:
selector = null;
conditions = null;
conditions = [];
condition = new CSSCondition("class", "Button1");
conditions.push(condition);
selector = new CSSSelector("spark.components.Button", conditions, selector);
// spark.components.Button.Button1
style = styleManager.getStyleDeclaration("spark.components.Button.Button1");
if (!style)
{
style = new CSSStyleDeclaration(selector, styleManager);
}
if (style.factory == null)
{
style.factory = function():void
{
this.backgroundImage = _embed_css____assets_ui_add_png_867674143;
};
}
//
_TestProject_FlexInit
[Mixin]类,被写入SystemManager::info()["minxins"]中,在SystemManager初始货完成加载Application之前(SystemManager::kickoff()中)调用静态的init方法
此类完成各种单例、效果、别名等的注册
//
_TestProject_Styles
[Mixin]类,被写入SystemManager::info()["minxins"]中,在SystemManager初始货完成加载Application之前(SystemManager::kickoff()中)调用静态的init方法
此类完成系统内置组件样式、皮肤的绑定,如:
selector = null;
conditions = null;
conditions = null;
selector = new CSSSelector("spark.components.Button", conditions, selector);
mergedStyle = styleManager.getMergedStyleDeclaration("spark.components.Button");
style = new CSSStyleDeclaration(selector, styleManager, mergedStyle == null);
if (style.defaultFactory == null)
{
style.defaultFactory = function():void
{
this.skinClass = spark.skins.spark.ButtonSkin;
};
}
if (mergedStyle != null &&
(mergedStyle.defaultFactory == null ||
ObjectUtil.compare(new style.defaultFactory(), new mergedStyle.defaultFactory())))
{
styleManager.setStyleDeclaration(style.mx_internal::selectorString, style, false);
}
///////////////////////////////////
根据这些信息,可以思考如何优化flex工程的编译速度,及缩减项目swf的大小
订阅:
博文 (Atom)

