2010-11-09 72 views
0

我正在使用Flash Builder 4 Burrito预览 - 编译移动应用程序。我有一个名为footer.mxml的自定义组件。该页脚有4个按钮,其中一个标签绑定了cartValue。我试图在所有视图和页脚组件中维护一个名为cartValue的全局变量。使用全局变量的Flex组件

footer.mxml

<s:Group xmlns:fx="http://ns.adobe.com/mxml/2009" 
    xmlns:s="library://ns.adobe.com/flex/spark" 
    xmlns:mx="library://ns.adobe.com/flex/mx" 
    width="100%" height="64" chromeColor="#000000" fontSize="10"> 
<fx:Declarations> 
    <!-- Place non-visual elements (e.g., services, value objects) here --> 
    <mx:CurrencyFormatter id="currencyFormatter" 
    currencySymbol="$" 
    useThousandsSeparator="true" 
    precision="2" /> 
</fx:Declarations> 



<fx:Script> 
    <![CDATA[ 
    [Bindable] 
    public var cartValue:int; 

    ]]> 
</fx:Script> 

<s:HGroup width="100%" contentBackgroundColor="#000000" paddingBottom="0" paddingLeft="0" 
paddingRight="0" paddingTop="0"> 
<s:Button x="0" y="624.5" width="25%" height="64" label="Account" chromeColor="#2259AA" 
enabled="true" fontSize="10" fontWeight="bold" icon="@Embed('assets/user.png')"/> 
<s:Button x="121" y="624.5" width="25%" height="64" label="Orders" chromeColor="#2259AA" 
fontSize="10" icon="@Embed('assets/doc_lines_stright.png')"/> 
<s:Button x="241" y="624.5" width="25%" height="64" label="Help" chromeColor="#2259AA" 
fontSize="10" icon="@Embed('assets/spechbubble.png')"/> 
<s:Button x="360" y="624.5" width="25%" height="64" label="{currencyFormatter.format(cartValue)}" chromeColor="#2259AA" 
fontSize="10" icon="@Embed('assets/shop_cart.png')"/> 

</s:HGroup> 
    </s:Group> 

RincoTest.mxml

<s:MobileApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
    xmlns:s="library://ns.adobe.com/flex/spark" 
    xmlns:mx="library://ns.adobe.com/flex/mx" 
    backgroundColor="#000000" firstView="views.RincoTestHome" 
    > 
<fx:Style source="RincoTest.css"/> 
<fx:Declarations> 
<!-- Place non-visual elements (e.g., services, value objects) here --> 
</fx:Declarations> 
<fx:Script> 
    <![CDATA[ 

    [Bindable] 
    public var cartValue:int; 
    ]]> 
</fx:Script> 


<s:titleContent> 
    <s:Image left="1" top="3" width="173" height="75" backgroundAlpha="1.0" smooth="true" 
source="assets/iphone_large.png"/> 
</s:titleContent> 
<s:navigationContent> 
    <mx:Spacer width="10" height="82"/> 
</s:navigationContent> 


    </s:MobileApplication> 

这是怎么了实现它

<components:footer x="1.65" y="614.95" width="100%" height="64" cartValue="{cartValue}"/> 

我试图绑定Application.application.cartValue和 MobileApplication.application.cartValue。它们都不工作。

如果在整个应用程序中有更好的方法来维护cartValue,请告诉我。这是我第一次尝试使用Flex。

感谢, 厄尼

回答

1

在组件使用一个静态变量。

将其引用为ComponentName.staticVar。通常,需要了解全局的所有信息都已知道(例如导入)组件文件。

干杯

+0

假设我想存储一个全局变量,因此所有的意见可以访问它。什么是最好的方法? – cfEngineers 2011-01-06 18:43:16

1

另一种解决方案是使用一个单例模式:单实例,通过静态访问所有引用。

效果大致相同。通过静态属性对实例进行静态引用的唯一好处是该实例可以是继承的一部分,并且可以实现接口。

我也有一些间歇性问题,结合静态值,但可能是以前的版本,PEBCAK等

干杯