【问题标题】:Flex Component using global variable使用全局变量的 Flex 组件
【发布时间】:2011-05-07 07:59:32
【问题描述】:

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

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。

谢谢, 厄尼

【问题讨论】:

    标签: flex4 global-variables flash-builder custom-component


    【解决方案1】:

    在组件中使用静态变量。

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

    干杯

    【讨论】:

    • 假设我想存储一个全局变量,以便所有视图都可以访问它。最好的方法是什么?
    【解决方案2】:

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

    效果差不多。静态引用实例相对于静态属性的唯一优势是实例可以是继承的一部分并且可以实现接口。

    我在绑定到静态值时也遇到了一些间歇性问题,但这可能是以前的版本、PEBCAK 等。

    干杯

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-02
      • 1970-01-01
      • 2020-11-02
      • 2020-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多