从未在简单的应用程序中调用FlexEvent.APPLICATION_COMPLETE

时间:2010-07-03 23:44:07

标签: actionscript-3 flex flash-builder mxml

我有一个非常简单的flex应用程序,主文件为Rec.mxml

<?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="215" minHeight="138" width="215" height="138" backgroundAlpha="0.0">

 <fx:Script>
  <![CDATA[
   var rec:LRec = new LRec();
  ]]>
 </fx:Script>

 <fx:Declarations>
  <!-- Place non-visual elements (e.g., services, value objects) here -->
 </fx:Declarations>
</s:Application>

AS3课程:

package
{
 import mx.core.Application;
 import mx.events.FlexEvent;

 public class LRec extends Application
 {

  public function LRec()
  {
   trace("CONSTRUCTED"); 
   addEventListener(FlexEvent.APPLICATION_COMPLETE, this.mainInit); 
  }

  /**
   * After the application startup is complete, debug
   */
  public function mainInit(event:FlexEvent):void {
   trace("COMPLETE");
  }
 }
}

打印trace ("CONSTRUCTED"),但不打印"COMPLETE" - 看起来FlexEvent.APPLICATION_COMPLETE事件永远不会注册。有什么想法吗?

此外,这是我第一次真正完成这种编程,所以如果其他任何错误,请告诉我!

1 个答案:

答案 0 :(得分:2)

我已经更新了您的代码,并对您出错的地方发表了一些评论。如果您有任何其他问题,请随时提出。

Test.mxml

<?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="215" minHeight="138" width="215" height="138" backgroundAlpha="0.0"
    initialize="handle_initialize(event)"
>

    <fx:Script>
        <![CDATA[
            import mx.events.FlexEvent;

            // Probably want to keep your rec object around
            private var rec:LRec;

            // Construct your LRec instance in the Application's inititialize state,
            // rather than declaring it statically.
            protected function handle_initialize(event:FlexEvent):void
            {
                // Construct a new LRec instance, assign it to our private variable.
                this.rec = new LRec();

                // The <s:Applicaiton instance we define in this mxml is the object that 
                // will actually dispatch the applicationComplete event, so we want to
                // listen for that here and pass it on to our LRec instance's mainInit 
                // method when it fires.
                this.addEventListener(FlexEvent.APPLICATION_COMPLETE, this.rec.mainInit);
            }
        ]]>
    </fx:Script>

    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>
</s:Application>

LRec.as

package
{
    import mx.events.FlexEvent;

    // Don't inherit from Application, as you've already defined your application
    // in your mxml, and you should have only one Application instance per application.
    public class LRec
    {

        public function LRec()
        {
            trace("CONSTRUCTED");

            // Doesn't need the event listener anymore.

            // Do any instantiation needed.
        }

        /**
         * After the application startup is complete, debug
         */
        public function mainInit(event:FlexEvent):void {
            trace("COMPLETE");

            // Do any of the work that needs to wait for the applicationComplete
            // event to fire.
        }
    }
}

Here's Flex实例化模型上的一篇好文章(虽然有点过时)。