代码之家  ›  专栏  ›  技术社区  ›  rxmnnxfpvg

flexevent.application_complete在简单应用程序中从未调用过

  •  0
  • rxmnnxfpvg  · 技术社区  · 14 年前

    我有一个非常简单的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 回复  |  直到 9 年前
        1
  •  2
  •   Sam Dolan    14 年前

    我更新了您下面的代码,并对您的错误进行了一些评论。如果你还有其他问题,可以随时问。

    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实例化模型的好文章(虽然有点过时)。