从fx:component flex中调用AS内部的函数

时间:2012-06-14 18:45:48

标签: xml actionscript-3 flex mobile mxml

这个可能很简单,但我无法在网上找到答案。继承人我所拥有的:

<fx:Script>
        <![CDATA[
            protected function DestIsAirport(event:MouseEvent):void {
                navigator.pushView(AirportSelector);
            }
            protected function DestIsAddress(event:MouseEvent):void {
                navigator.pushView(DestinationInfoView);
            }
        ]]>
</fx:Script>

然后我在它下方有一个弹出式容器,它看起来像这样:

<fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
        <fx:Component className="DestAlert">
            <s:SkinnablePopUpContainer x="50" y="200">
                <s:TitleWindow title="Destination">
                    <s:VGroup horizontalAlign="center" verticalAlign="middle" paddingTop="8" paddingBottom="8" paddingLeft="8" paddingRight="8" gap="5" width="100%">
                        <s:Label text="Is your destination an airport?"/>
                    </s:VGroup>
                    <s:HGroup horizontalAlign="center" verticalAlign="middle" paddingTop="40" paddingBottom="8" gap="5" width="100%">
                        <s:Button label="Yes" bottom="10" left="10" width="100" fontSize="16" click="DestIsAirport(event)"/>
                        <s:Button label="No" bottom="10" right="10" width="100" fontSize="16" click="DestIsAddress(event)"/>
                    </s:HGroup>
                </s:TitleWindow>
            </s:SkinnablePopUpContainer>
        </fx:Component>
    </fx:Declarations>

我希望这两个按钮能够运行AS3脚本中的不同功能。这会产生错误: 1180:调用可能未定义的方法DestIsAddress。 1180:调用可能未定义的方法DestIsAirport。

如何让这些按钮运行这两个功能?

整个源文件:

<?xml version="1.0" encoding="utf-8"?>
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark" title="Enter Pick Up Address">

    <fx:Script>
        <![CDATA[
            protected function button1_clickHandler(event:MouseEvent):void
            {
                navigator.popView();
            }
            protected function DestIsAirport(event:MouseEvent):void {
                navigator.pushView(AirportSelector);
            }
            protected function DestIsAddress(event:MouseEvent):void {
                navigator.pushView(DestinationInfoView);
            }
        ]]>
    </fx:Script>
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
        <fx:Component className="DestAlert">
            <s:SkinnablePopUpContainer x="50" y="200">
                <s:TitleWindow title="Destination">
                    <s:VGroup horizontalAlign="center" verticalAlign="middle" paddingTop="8" paddingBottom="8" paddingLeft="8" paddingRight="8" gap="5" width="100%">
                        <s:Label text="Is your destination an airport?"/>
                    </s:VGroup>
                    <s:HGroup horizontalAlign="center" verticalAlign="middle" paddingTop="40" paddingBottom="8" gap="5" width="100%">
                        <s:Button label="Yes" bottom="10" left="10" width="100" fontSize="16" click="close()"/>
                        <s:Button label="No" bottom="10" right="10" width="100" fontSize="16" click="close()"/>
                    </s:HGroup>
                </s:TitleWindow>
            </s:SkinnablePopUpContainer>
        </fx:Component>
    </fx:Declarations>
    <s:actionContent>
        <s:Button label="Back" click="button1_clickHandler(event)"/>
    </s:actionContent>
    <s:TextInput top="6" horizontalCenter="-1" prompt="Address Line 1"/>
    <s:TextInput top="47" horizontalCenter="-1" prompt="Address Line 2"/>
    <s:RadioButton top="90" label="IL - Illinois" horizontalCenter="-94"/>
    <s:RadioButton top="90" label="WI - Wisconsin" horizontalCenter="72"/>
    <s:SpinnerListContainer bottom="60" width="320" height="211" horizontalCenter="0">
        <s:SpinnerList width="317" height="100%" labelField="data" selectedIndex="0">
            <s:ArrayList>
                <fx:Object data=""></fx:Object>
                <fx:Object data="Abbott Park"></fx:Object>
                <fx:Object data="Addison"></fx:Object>
                <fx:Object data="Algonquin"></fx:Object>
                <fx:Object data="Alsip"></fx:Object>

    </s:ArrayList>
        </s:SpinnerList>
    </s:SpinnerListContainer>
    <s:Button bottom="10" width="302" label="Next" click="(new DestAlert()).open(this, false)"
              fontSize="24" horizontalCenter="0"/>
</s:View>

2 个答案:

答案 0 :(得分:1)

声明部分仅适用于用户不会看到的内容。你需要做几件事。

  1. 获取声明部分中的组件并将其移动到自己的文件中。
  2. 通过弹出式管理器显示组件的实例。
  3. 此外,重要的是要注意组件和父组件将在完全不同的范围内。但是,在创建弹出组件的新实例后,您可以使用它的引用来添加侦听器。然后,组件可以调度此类可以侦听的事件,然后调用这些函数。

    这变得更加复杂,因为popupmanager是一个静态类,与你在这里添加的代码无关。如果这需要更多解释,请告诉我,因为我几乎觉得图片是有序的。

答案 1 :(得分:0)

为什么不创建单独的动作脚本/ mxml文件?这很容易解决这个问题。