`
bufanliu
  • 浏览: 197383 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

programming Flex2 学习笔记-第5章-框架基础.txt (转载的,来自)

    博客分类:
  • FLEX
阅读更多
来自http://blog.chinaunix.net/u/19419/showart_431067.html
Framework Fundamentals框架基础
理解Flex应用程序的生命周期
Flex应用的根是SystemManger,它是flash.display.MovieClip的子类,一个Flash Player显示对象类型。SystemManager有两个帧,第一帧是用来显示应用载入的进度指示,这个帧是轻量的,所以它几乎能立即下载和运行。第二帧便是应用本身。
当一个Flex应用的SystemManager实例进入到第二帧,它创建一个主应用的实例。SystemManager实例有一个application属性,在它第二帧创建应用对象之前,它是null的。在那个点,应用实例被初始化并运行它自己的启动处理。意指所有的应用对象内部的生命周期事件发生。内部的生命周期事件为:
preinitialize应用被初始化,但仍没创建任何子组件。
initialize应用已创建子组件,但极布局这些组件。
creationComplete应用已被初始实例化完成,并已布局所有的组件。
一旦应用完成它的内部启动处理,它通过分发applicationComplete事件通知SystemManager。从这个点之后,应用准备运行了。
SystemManager也管理所以被在前台显示的内容,即所有的pop ups,光标,工具提示。
SystmeManager有一个toplevelSystemManager的属性,引用到当时在FlashPlayer里运行的根级的SystemManager实例。当一个应用是作为FlashPlayer的主应用被载入的,这个属性总是自引用的,然而,当一个应用是被另一个应用载入的,这个被载入应用的SystemManager对象的topLevelSystemManager则是引用到父应用。
虽然你不常要引用SystemManager,但需要时可以做。所有的UIComponents(包括Application)的子类都有一个systemManager属性引用到application的SystemManager。开发者常喜欢使用SystemManager来监听应用中任何显示对象分发的事件。当事件冒泡时,有机会操控(handle)事件的最后的对象便是SystemManager.
FlashPlayer和框架的不同
FlasyPlayer是Flash和Flex应用的运行环境。它能运行.swf文件。该文件包含这些字节代码:能与FlashPlayer通信,命令它执行载入图像,画图,发起http请求等等操作。Flash和Flex应用只能做那些FlashPlayer允许他们做的,FlashPlayer提供可执行的API.
应用只包含命令,而FlashPlayer则负责运行这些命令,所以Flash和Flex应用的不同不是内容,而是怎样创建内容。
使用框架的代价是.swf文件尺寸的增长,这与用纯AS写的项目形成反差。因为当你不用Flex框架,你是直接引用FlashPlayer的主类,这些类已经在FlashPlayer中了,他们不需被编译进.swf文件。在用Flex框架时,简单的增加一个组件也会增加很多的文件尺寸,因为它需要编译的一个类或一个类库并不是FlashPlayer的一部分。
若类的包是以flash.开头,它是FlashPlayer的一部分;
若类的包是以mx.开关,它是Flex框架的一部分;
MXML标签几乎总是(少数的例外)对应Flex框架类。

Bootstarapping Flex Applications
一个Flex应用的根并不是一个Application对象,实际上一个Application标签创建的是一个mx.managers.SystemManager对象。
flash.display.MovieClip是一个显示对象,允许你对时间轴编程。但在Flex应用中并不怎用时间轴,因为没有编程方法增加一个帧到到个时间轴。但时间轴和帧仍是SystmeManager的基本部分之一。
因为没有方法编程的增加帧,几乎Flex应用中的所有显示对象只由一个帧组成,但SystemManager是个例外,它有两个帧,一个用于预装载,主应用对象存在第二帧中。一般情况下,你不需知道这些,但在至少两个情况下你需了解它:在一个Flex应用中载入另一个Flex应用;定制预装载器。
Loading One Flex Application into Another Flex Application
当一个SWFLoader载入一个Flex 应用,这个SWFLoader对象的contern属性提供了对被载入应用的根的引用,也即是被载入应用的SystemManager对象。这个SystemManagr类定义一个application属性可引用Application对象。但这个属性在Flex 应用被载入时还是null,在第二帧之前,它是不会被创建的。那要怎样引用它呢?有个优雅的方法可以解决这个问题:
当一个SWFLoader载入和初始化了内容,它分发init事件,在init的事件处理里,你能够引用被载入内容的SystemManager了,此时你就增加一个事件侦听器,监听这个SystemManager的applicationComplete事件。而在这个applicationComplete的事件触发之后,在它的处理里,你就可以引用被进入内容的Application对象了。
例子:
要被载入的应用:B.MXML
<?xml version="1.0" encoding="utf-8"?>
     <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
         <mx:Script>
             <![CDATA[
                 public function setBackground(color:Number):void {
                     canvas.setStyle("backgroundColor", color);
                 }
             ]]>
         </mx:Script>
         <mx:Canvas id="canvas" backgroundColor="#FFFFFF" width="100" height="100" />
     </mx:Application>
父应用:
<?xml version="1.0" encoding="utf-8"?>
     <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
         <mx:Script>
             <![CDATA[
                 import mx.managers.SystemManager;
                 import mx.events.FlexEvent;
                 private function initHandler(event:Event):void {
                        event.target.content.addEventListener(FlexEvent.APPLICATION_COMPLETE,
     applicationCompleteHandler);
                 }
                 private function applicationCompleteHandler(event:Event):void {
                     event.target.application.setBackground(0xFFFF00);
                 }
             ]]>
         </mx:Script>
         <mx:SWFLoader source="B.swf" init="initHandler(event)" />
     </mx:Application>
提示:
Flex 2.0.1内建的特性创建一个模块应用,在运行时将几个.swf文件缝装在一起。在许多情况下,使用模块是更简单的方法。
Understanding Application Domains
一个应用域是运行在FlashPlayer中的分隔的一个应用?????????。在多数情况下,只有一个应用运行在FlashPlayer中,这时只有一个应用域。但当你在一个已存在的应用中载入附加的.swf文件时,你可能需要为附加的应用创建新的应用域。
All Flex and Flash applications are composed of collections of classes. An applica-
tion domain holds the collections of classes for an application or applications. When
just one application is running in Flash Player, the concept of an application domain
is practically a formality because you are guaranteed that an .swf will never contain
more than one definition for a class. However, when you load an additional .swf file,
there is a possibility that it will contain a definition for a class by the same name as
one that is already loaded from another .swf file. An application domain ensures that
within the domain there is only one definition for each class. Therefore, it has a set of
rules  for  determining  how  to  choose  between  conflicting  definitions  if  such  a  sce-
nario presents itself.
If  an  application  is  loaded  into  an  application  domain  with  a  parent,  it  essentially
inherits  all  the  class  definitions  from  the  parent  application  domain.  The  result  is
that  the  child  application  domain  cannot  have  class  definitions  for  classes  that  are
otherwise  defined  in  the  parent  application  domain.  For  example,  if  you  load  one
Flex  application  .swf  into  another  Flex  application  .swf  with  the  default  settings,
there would be two application domains but one would be a child of the other, and
all duplicate Flex framework classes from the child would be disregarded in favor of
the same classes from the parent application domain. This is often appropriate, and
it has several possible benefits:
  *   It uses less memory. If the duplicate classes were not disregarded, memory usage
     would increase.
  *   Singleton manager classes are accessible to both the parent and the child applica-
     tions (meaning that just one instance of the class is shared by parent and child
     applications).
  *   Theoretically, it is possible to compile the child .swf files by excluding any dupli-
     cate classes the child .swf would inherit at runtime from the parent application
     domain. This would reduce the file size overhead in child .swf files.
Just as there are cases in which this default child domain behavior is useful, some-
times  it  works  at  cross  purposes  with  the  needs  or  requirements  of  a  project.  For
example, consider the scenario in which two applications are built using two classes
with  the  same  name  but  very  different  implementations.  If  one  is  loaded  into  the
other, the child will not work as intended because that class will be discarded in the
child, and the parent version will be used in both applications. In such a case, it is
clear that there is a need to be able to completely partition the applications into sepa-
rate application domains. Separate application domains ensure that the sorts of con-
flicts just  described    don’t   occur.   However,     it is  important    to  use   these  sorts  of
exclusive application domains only when necessary because they will increase mem-
ory usage.
第1种:被载入的.swf文件运行在一个新的应用域,作为已存在应用域的child。
第2种:被载入的.swf文件运行在一个新的应用域
第3种:一个.swf文件被载入到相同应用域。用于运行时共享库,当你想运行时载入字体和其它的assets库在主应用里使用时,它也是有用的。
例子:
     var context:LoaderContext = new LoaderContext();
     context.applicationDomain = new ApplicationDomain(ApplicationDomain.currentDomain); 将当前应用域作为新应用域的父应用域
//     context.applicationDomain = new ApplicationDomain();新建一个分离的独立的应用域
////     context.applicationDomain = ApplicationDomain.currentDomain; 使用当前的应用域
     var request:URLRequest = new URLRequest("RuntimeLoadingExample.swf");
     var loader:Loader = new Loader();
     loader.load(request, context);
理解预加载器Understanding the Preloader
默认的,所有的Flex applications都有一个预加载器,通过一个进度条指示应用加载和初始化的进度。通常的,进度条注册一个或多个的侦听器,为preloader对象分发的一系列事件,以下是preloader的有效事件:
progress指示下载进度
complete指示下载完成
rslError指示运行时共享库不能被载入
rslProgress指示运行时共享库的下载进度
rslComplete指示运行时共享库的下载完成
initProgress指示应用正在初始化
initComplete指示应用已经初始化
一旦,system manager进入到第二帧,应用进行自身的创建和初始化。.....
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics