Flex加载模块READY事件没有被触发,为什么?

时间:2009-10-12 21:45:19

标签: flex flash module

您好我有这个非常简单的mxml示例。

如果模块是远程的http://edofiles.s3.amazonaws.com/calculator.swf

,则使用flex builder与flex sdk 3.4.0

READY事件永远不会被解雇,我不明白为什么。

你有同样的行为吗?

原始代码来自http://lowpitch.com/blog/modulemanager-and-imoduleinfo-loading-flex-modules-dynamically/#comment-4396

任何线索?

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
    applicationComplete="initApp()">
    <mx:Script>
        <![CDATA[
            import mx.modules.Module;
            import mx.events.ModuleEvent;
            import mx.modules.ModuleManager;
            import mx.modules.IModuleInfo;

          protected var _moduleInfo:IModuleInfo;

            private function initApp():void {
               addToLog ("Application initialised");
               // create the module - note, we're not loading it yet
                _moduleInfo = ModuleManager.getModule("http://edofiles.s3.amazonaws.com/calculator.swf");
               // add some listeners
               _moduleInfo.addEventListener(ModuleEvent.READY, onModuleReady);
               _moduleInfo.addEventListener(ModuleEvent.SETUP, onModuleSetup);
               _moduleInfo.addEventListener(ModuleEvent.UNLOAD, onModuleUnload);
               _moduleInfo.addEventListener(ModuleEvent.PROGRESS, onModuleProgress);
            }

            protected function getModuleInfo () : IModuleInfo {
               // return the module info
               return _moduleInfo;
            }

            /**
             * Adds output to the log
             **/
            protected function addToLog (msg:String) : void {
                log.text += msg + "\n";
                // scroll to the bottom on the next frame
                callLater(scrollToBottom);

            }

            protected function scrollToBottom () : void {
                // scroll to the bottom
                log.verticalScrollPosition = log.maxVerticalScrollPosition;
            }

            /**
            * Called when the "load" button is pressed
            *
            * Starts loading the module - when the module has been
            * loaded, an instance of the module will be created
            * and added to the tile control
            *
            **/
            private function loadModule () : void {
                addToLog ("Loading module");
                // load the module
                getModuleInfo().load();
            }

            /**
             * Called when the "unload" button is pressed
             *
             * Removes all the instances of the module from the
             * tile control, then unloads the module
             *
             */
            private function unloadModule () : void {
                addToLog ("Unloading module");
                // clear out all the the instances
                // of the module from the tile
                tile.removeAllChildren();
                // unload the module
                getModuleInfo().release();
                //getModuleInfo().un
            }

            /**
            * Handler for the ModuleEvent.PROGRESS event
            **/
            protected function onModuleProgress (e:ModuleEvent) : void {
                addToLog ("ModuleEvent.PROGRESS received: " + e.bytesLoaded + " of " + e.bytesTotal + " loaded.");
            }

            /**
            * Handler for the ModuleEvent.SETUP event
            **/
            private function onModuleSetup (e:ModuleEvent) : void {
                addToLog ("ModuleEvent.SETUP received");
                // cast the currentTarget
                var moduleInfo:IModuleInfo = e.currentTarget as IModuleInfo;
                addToLog ("Calling IModuleInfo.factory.info ()");
                // grab the info and display information about it
                var info:Object = moduleInfo.factory.info();
                for (var each:String in info) {
                    addToLog ("     " + each + " = " + info[each]);
                }

            }

            /**
            * Handler for the ModuleEvent.READY event
            **/
            private function onModuleReady (e:ModuleEvent):void {
                addToLog ("ModuleEvent.READY received");
                // cast the currentTarget
                var moduleInfo:IModuleInfo = e.currentTarget as IModuleInfo;
                // Add an instance of the module's class to the
                // display list.
                addToLog ("Calling IModuleInfo.factory.create ()");
                tile.addChild( moduleInfo.factory.create () as Module);
                addToLog ("SomeModule instance created and added to Display List");
            }

            /**
            * Handler for the ModuleEvent.UNLOAD event
            **/
            public function onModuleUnload (e:ModuleEvent) : void {
                addToLog ("ModuleEvent.UNLOAD received");
            }

        ]]>
    </mx:Script>

    <mx:HBox width="100%" height="100%">
        <mx:Tile id="tile" width="100%" height="100%" />
           <mx:TextArea id="log" width="100%" height="100%"/>
    </mx:HBox>


    <mx:ApplicationControlBar>
        <mx:Button label="Load" click="loadModule ()" />
        <mx:Button label="Unload" click="unloadModule ()"/>
    </mx:ApplicationControlBar>

</mx:Application>

3 个答案:

答案 0 :(得分:3)

我今天有这种行为。即使它与海报所要求的内容没有直接关系,但谷歌搜索中的这个主题相当高,所以我想帮助那些在这里绊倒的人。

我在以下博客中找到了解决方案: http://www.joshuaostrom.com/2008/08/14/flex-modules-watch-your-scope/

问题在于,当你调用load()时,ModuleLoader会自动收集垃圾,所以如果你不太注意你的引用,你的听众就会被摧毁。

答案 1 :(得分:0)

原因可以简单而有趣。尝试taht:首先是addEventListener代码,然后是getModule()。

答案 2 :(得分:0)

它也可能发生,因为您尝试使用为调试编译的应用程序加载为发布而编译的模块。这件事发生在我之前。 app和modules都必须匹配debug / debug或release / release。

如果它们不匹配,您将获得进度事件和“设置”事件,但从未进行准备事件。