简体   繁体   English

在 <fx:Script> 标签

[英]calling a function in the <fx:Script> tags

I am a bit stumped. 我有点难过。 I thought it was possible to call a function within the fx:Script tag simply by referencing the function ("display_album()" as in the code below). 我认为仅通过引用函数(如下面的代码中的“ display_album()”)就可以在fx:Script标记内调用函数。 It would make sense to have that function call outside the curly braces, but when I do, the debugger in FlashBuilder gives me the 1180 error, call to a possibly undefined method. 在花括号之外进行该函数调用会很有意义,但是当我这样做时,FlashBuilder中的调试器给我1180错误,调用了一个可能未定义的方法。

I can call the function with a button click (which makes sense too), and I get the proper trace in the FlashBuilder debugger. 我可以通过单击按钮来调用该函数(这也很有意义),然后在FlashBuilder调试器中获得正确的跟踪。

But I am curious as to how I can call a function in the tag without adding a button. 但是我很好奇如何在不添加按钮的情况下调用标签中的函数。 Thanks! 谢谢!

 <?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>

    <fx:Script>
        <![CDATA[

            public function display_album():void
            {
                var album:String = "The White Album";
                trace (album);

            }

            display_album();

        ]]>
    </fx:Script>
    <s:Button x="192" y="259" label="Button" click = "display_album()"/>

</s:Application>
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
           xmlns:s="library://ns.adobe.com/flex/spark" 
           xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"
           initialize="init()">
<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>

<fx:Script>
    <![CDATA[
        private function init():void
        {
          display_album();
        }

        public function display_album():void
        {
            var album:String = "The White Album";
            trace (album);

        }

    ]]>
</fx:Script>
<s:Button x="192" y="259" label="Button" click = "display_album()"/>

I hope this helps. 我希望这有帮助。

You can't call an instance function like that, because it runs before the constructor is called. 您不能调用这样的实例函数,因为它在调用构造函数之前运行。 Since the instance itself has not been initialized yet, there is no scope within with this function can be executed. 由于实例本身尚未初始化,因此该函数中没有作用域可以执行。 You can only call class functions before the constructor is called. 您只能在调用构造函数之前调用类函数。

You can either convert the method into a class method through the static keyword (probably not right in this case), or trigger this method on the creationComplete event. 您可以通过static关键字将方法转换为类方法(在这种情况下可能不正确),也可以在creationComplete事件上触发此方法。

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

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