天天看點

programming Flex2 學習筆記-第5章-架構基礎.txt (轉載的,來自)

來自[url]http://blog.chinaunix.net/u/19419/showart_431067.html[/url]

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進入到第二幀,應用進行自身的建立和初始化。.....

繼續閱讀