简体   繁体   English

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

[英]Calling a function inside AS from fx:component flex

This one is probably really simple, but I can't find online an answer. 这可能真的很简单,但我无法在网上找到答案。 Heres what I have: 这是我所拥有的:

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

and then I have a pop up container just below that and it looks like this: 然后在它的下面有一个弹出容器,它看起来像这样:

<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>

I want the two buttons to run different functions from the AS3 script. 我希望这两个按钮运行与AS3脚本不同的功能。 This produces an error: 1180: Call to a possibly undefined method DestIsAddress. 这将产生错误:1180:调用可能未定义的方法DestIsAddress。 1180: Call to a possibly undefined method DestIsAirport. 1180:调用可能未定义的方法DestIsAirport。

How do I get those buttons to run those two functions? 如何获得这些按钮来运行这两个功能?

Entire source file: 整个源文件:

<?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>

The declarations section is only meant for things the user isn't going to see. 声明部分仅用于用户不会看到的内容。 You need to do a couple of things. 您需要做几件事。

  1. Take the component in the declarations section and move it into it's own file. 将组件放入声明部分,并将其移入其自己的文件中。
  2. Show an instance of the component via a popup manager. 通过弹出管理器显示组件的实例。

Additionally, it's important to note that the component and the parent are going to be in completely different scopes. 另外,需要注意的是,组件和父组件将处于完全不同的范围内。 However, after creating a new instance of the popup component, you can use your reference to it to add listeners. 但是,在创建弹出组件的新实例之后,可以使用对其的引用来添加侦听器。 The component can then dispatch events that this class can listen to, which then call the functions. 然后,组件可以调度此类可以侦听的事件,然后调用这些函数。

This gets more complicated because the popupmanager is a static class and will have no relation to the code you've added here. 这变得更加复杂,因为popupmanager是一个静态类,并且与您在此处添加的代码无关。 Let me know if this needs more explanation, because I almost feel like a picture is in order. 让我知道是否需要更多说明,因为我几乎感觉像是在整理照片。

Why not creating a separate action script/mxml file? 为什么不创建单独的动作脚本/ mxml文件? That would easily solve that issue. 那很容易解决这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM