Posted on 2007-04-28 12:38
云自無心水自閑 閱讀(6340)
評論(6) 編輯 收藏 所屬分類:
心得體會 、
Flex2
首先是應用的代碼, 在應用中使用 <mx:ModuleLoader >來加載模塊
<?xml version="1.0"?>
<!-- modules/URLModuleLoaderApp.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" viewSourceURL="srcview/index.html">

<mx:Panel
title="Module Example"
height="90%"
width="90%"
paddingTop="10"
paddingLeft="10"
paddingRight="10"
paddingBottom="10"
>
<mx:Label width="100%" color="blue"
text="Select the tabs to change the panel."/>
<mx:TabNavigator id="tn"
width="100%"
height="100%"
creationPolicy="auto"
>
<mx:VBox id="vb1" label="Column Chart Module">
<mx:Label id="l1" text="ColumnChartModule.swf"/>
<mx:ModuleLoader url="ColumnChartModule.swf"/>
</mx:VBox>
<mx:VBox id="vb2" label="Pie Chart Module">
<mx:Label id="l2" text="piehchartmodule.swf"/>
<mx:ModuleLoader url="piechartmodule.swf"/>
</mx:VBox>
<mx:VBox id="vb3" label="Line Chart Module">
<mx:Label id="l3" text="linehchartmodule.swf"/>
<mx:ModuleLoader url="linechartmodule.swf"/>
</mx:VBox>
</mx:TabNavigator>
</mx:Panel>

</mx:Application>
在這個應用中主要是一個TagNavigator, 里面有三個標簽頁. 每個標簽頁加載一個模塊.
下面是其中一個模塊的代碼:
<?xml version="1.0"?>
<!--ColumnChartModule.mxml -->
<mx:Module xmlns:mx="http://www.adobe.com/2006/mxml" width="100%" height="100%" >

<mx:Script><![CDATA[
import mx.collections.ArrayCollection;
[Bindable]
public var expenses:ArrayCollection = new ArrayCollection([

{Month:"Jan", Profit:2000, Expenses:1500},

{Month:"Feb", Profit:1000, Expenses:200},

{Month:"Mar", Profit:1500, Expenses:500}
]);
]]></mx:Script>
<mx:ColumnChart id="myChart" dataProvider="{expenses}">
<mx:horizontalAxis>
<mx:CategoryAxis
dataProvider="{expenses}"
categoryField="Month"
/>
</mx:horizontalAxis>
<mx:series>
<mx:ColumnSeries
xField="Month"
yField="Profit"
displayName="Profit"
/>
<mx:ColumnSeries
xField="Month"
yField="Expenses"
displayName="Expenses"
/>
</mx:series>
</mx:ColumnChart>
<mx:Legend dataProvider="{myChart}"/>
</mx:Module>
最后, 應用和三個模塊一共會生成4個SWF. 一般來說, 應用使用延遲加載策略. 也就是說, 如果你打開應用后, 從來都不使用其中的某個模塊, 那個這個模塊永遠不會被加載. 這次做的好處是, 加快了第一次打開應用的速度, 但隨之而來的缺點就是, 第一次打開使用某個功能, 需要加載模塊時, 會需要一點等待的時間.