Actionscript 3 从XML文件加载flex模块
我正在尝试解析xml文件,并在应用程序中动态加载Flex模块。但它每次只加载最后一个模块。我有一个singleton类,用于解析和加载模块。这是上课时间Actionscript 3 从XML文件加载flex模块,actionscript-3,apache-flex,actionscript,Actionscript 3,Apache Flex,Actionscript,我正在尝试解析xml文件,并在应用程序中动态加载Flex模块。但它每次只加载最后一个模块。我有一个singleton类,用于解析和加载模块。这是上课时间 package { import flash.events.Event; import flash.net.URLLoader; import flash.net.URLRequest; import mx.controls.Alert; import mx.events.ModuleEvent; import mx.modules.IMod
package
{
import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLRequest;
import mx.controls.Alert;
import mx.events.ModuleEvent;
import mx.modules.IModuleInfo;
import mx.modules.ModuleLoader;
import mx.modules.ModuleManager;
public class VappModuleManager
{
private static var _instance:VappModuleManager;
private static const MODULE_PATH:String="./com/emc/vapp/";
private static const MANIFEST_PATH:String="module-manifest.xml";
private var _module:IModuleInfo;
private var _handler:Function;
private var loader:URLLoader;
public function VappModuleManager(object:SingletonEnforcer)
{
}
public function set handler(handler:Function):void
{
_handler=handler;
}
public static function get instance():VappModuleManager
{
if(_instance==null)
{
_instance=new VappModuleManager(new SingletonEnforcer());
}
return _instance;
}
public function load():void
{
loader = new URLLoader();
loader.addEventListener(Event.COMPLETE, xmlLoaded);
loader.load(new URLRequest(MANIFEST_PATH));
}
private function xmlLoaded(event:Event):void
{
Alert.show("Event Completed");
var manifest:XML=new XML(event.target.data);
Alert.show(manifest.module.length());
for (var index:int=0;index<manifest.module.length();index++)
{
Alert.show(MODULE_PATH+manifest.module[index].@name);
_module=ModuleManager.getModule(MODULE_PATH+manifest.module[index].@name);
_module.addEventListener(ModuleEvent.READY,_handler);
_module.load();
}
}
}
}
internal class SingletonEnforcer {}
我知道变量“\u module”的eventlistener存在问题,但不知道如何解决。非常感谢您的帮助。对
IModuleInfo.load
的调用是异步的,因此您的for
循环在加载任何模块之前已完全运行。此外,每次循环迭代时,类级别的\u模块
属性都会被新的模块
实例覆盖
我建议通过等待READY
事件顺序加载每个模块,并仅在下一个模块触发时启动加载。我还希望在加载所有模块时触发一个事件,而不是执行回调函数,因为这将给您带来更大的灵活性(例如,多个对象可以侦听一个事件)
以下内容未经测试,但应能让您了解:
package
{
import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLRequest;
import mx.controls.Alert;
import mx.events.ModuleEvent;
import mx.modules.IModuleInfo;
import mx.modules.ModuleLoader;
import mx.modules.ModuleManager;
public class VappModuleManager
{
private static var _instance:VappModuleManager;
private static const MODULE_PATH:String="./com/emc/vapp/";
private static const MANIFEST_PATH:String="module-manifest.xml";
private var loader:URLLoader;
private var manifest:XML;
private var loadCount:int = 0; // track loaded modules
public function VappModuleManager(object:SingletonEnforcer)
{
}
public static function get instance():VappModuleManager
{
if(_instance==null)
{
_instance=new VappModuleManager(new SingletonEnforcer());
}
return _instance;
}
public function load():void
{
loader = new URLLoader();
loader.addEventListener(Event.COMPLETE, xmlLoaded);
loader.load(new URLRequest(MANIFEST_PATH));
}
private function xmlLoaded(event:Event):void
{
manifest =new XML(event.target.data);
// load the first module
loadModule();
}
private function loadModule() {
// Use a locally scoped variable to avoid writing over the previous instance each time.
// You could push these onto an array if you need to be able to access them later
var module:IModuleInfo = ModuleManager.getModule(MODULE_PATH+manifest.module[loadCount].@name);
module.addEventListener(ModuleEvent.READY, moduleReadyHandler);
module.load();
}
private function moduleReadyHandler(event:ModuleEvent) {
// Remove the event listener on the loaded module
IModuleInfo(event.target).removeEventListener(ModuleEvent.READY, moduleReadyHandler);
loadCount ++;
// Are there still modules in the manifest to load?
if (loadCount < manifest.module.length()) {
// Yes... load the next module
loadModule();
} else {
// No... we're all finished so dispatch an event to let subscribers know
dispatchEvent(new Event("complete"));
}
}
}
}
包
{
导入flash.events.Event;
导入flash.net.urloader;
导入flash.net.URLRequest;
导入mx.controls.Alert;
导入mx.events.ModuleEvent;
导入mx.modules.IModuleInfo;
导入mx.modules.ModuleLoader;
导入mx.modules.ModuleManager;
公共类VappModuleManager
{
私有静态var_实例:VappModuleManager;
私有静态常量模块路径:String=“./com/emc/vapp/”;
私有静态常量清单路径:String=“module MANIFEST.xml”;
私有变量加载器:URLLoader;
私有var清单:XML;
私有变量loadCount:int=0;//跟踪加载的模块
公共函数VappModuleManager(对象:SingletonEnforcer)
{
}
公共静态函数get instance():VappModuleManager
{
if(_instance==null)
{
_instance=new-VappModuleManager(new-SingletonEnforcer());
}
返回_实例;
}
公共函数加载():void
{
loader=新的URLLoader();
loader.addEventListener(Event.COMPLETE,xmlLoaded);
load(新的URLRequest(MANIFEST_PATH));
}
私有函数xmlLoaded(事件:事件):void
{
manifest=新的XML(event.target.data);
//加载第一个模块
loadModule();
}
私有函数loadModule(){
//使用一个局部作用域变量,以避免每次都重写上一个实例。
//如果以后需要访问它们,可以将它们推送到阵列上
变量模块:IModuleInfo=ModuleManager.getModule(模块路径+清单.module[loadCount]。@name);
module.addEventListener(ModuleEvent.READY,moduleReadyHandler);
module.load();
}
私有函数moduleradyHandler(事件:ModuleEvent){
//删除已加载模块上的事件侦听器
IModuleInfo(event.target).removeEventListener(ModuleEvent.READY,moduleReadyHandler);
loadCount++;
//清单中还有要加载的模块吗?
if(loadCount
对IModuleInfo.load
的调用是异步的,因此在加载任何模块之前,for
循环已完全运行。此外,每次循环迭代时,类级别的\u模块
属性都会被新的模块
实例覆盖
我建议通过等待READY
事件顺序加载每个模块,并仅在下一个模块触发时启动加载。我还希望在加载所有模块时触发一个事件,而不是执行回调函数,因为这将给您带来更大的灵活性(例如,多个对象可以侦听一个事件)
以下内容未经测试,但应能让您了解:
package
{
import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLRequest;
import mx.controls.Alert;
import mx.events.ModuleEvent;
import mx.modules.IModuleInfo;
import mx.modules.ModuleLoader;
import mx.modules.ModuleManager;
public class VappModuleManager
{
private static var _instance:VappModuleManager;
private static const MODULE_PATH:String="./com/emc/vapp/";
private static const MANIFEST_PATH:String="module-manifest.xml";
private var loader:URLLoader;
private var manifest:XML;
private var loadCount:int = 0; // track loaded modules
public function VappModuleManager(object:SingletonEnforcer)
{
}
public static function get instance():VappModuleManager
{
if(_instance==null)
{
_instance=new VappModuleManager(new SingletonEnforcer());
}
return _instance;
}
public function load():void
{
loader = new URLLoader();
loader.addEventListener(Event.COMPLETE, xmlLoaded);
loader.load(new URLRequest(MANIFEST_PATH));
}
private function xmlLoaded(event:Event):void
{
manifest =new XML(event.target.data);
// load the first module
loadModule();
}
private function loadModule() {
// Use a locally scoped variable to avoid writing over the previous instance each time.
// You could push these onto an array if you need to be able to access them later
var module:IModuleInfo = ModuleManager.getModule(MODULE_PATH+manifest.module[loadCount].@name);
module.addEventListener(ModuleEvent.READY, moduleReadyHandler);
module.load();
}
private function moduleReadyHandler(event:ModuleEvent) {
// Remove the event listener on the loaded module
IModuleInfo(event.target).removeEventListener(ModuleEvent.READY, moduleReadyHandler);
loadCount ++;
// Are there still modules in the manifest to load?
if (loadCount < manifest.module.length()) {
// Yes... load the next module
loadModule();
} else {
// No... we're all finished so dispatch an event to let subscribers know
dispatchEvent(new Event("complete"));
}
}
}
}
包
{
导入flash.events.Event;
导入flash.net.urloader;
导入flash.net.URLRequest;
导入mx.controls.Alert;
导入mx.events.ModuleEvent;
导入mx.modules.IModuleInfo;
导入mx.modules.ModuleLoader;
导入mx.modules.ModuleManager;
公共类VappModuleManager
{
私有静态var_实例:VappModuleManager;
私有静态常量模块路径:String=“./com/emc/vapp/”;
私有静态常量清单路径:String=“module MANIFEST.xml”;
私有变量加载器:URLLoader;
私有var清单:XML;
私有变量loadCount:int=0;//跟踪加载的模块
公共函数VappModuleManager(对象:SingletonEnforcer)
{
}
公共静态函数get instance():VappModuleManager
{
if(_instance==null)
{
_instance=new-VappModuleManager(new-SingletonEnforcer());
}
返回_实例;
}
公共函数加载():void
{
loader=新的URLLoader();
loader.addEventListener(Event.COMPLETE,xmlLoaded);
load(新的URLRequest(MANIFEST_PATH));
}
私有函数xmlLoaded(事件:事件):void
{
manifest=新的XML(event.target.data);
//加载第一个模块
loadModule();
}
私有函数loadModule(){
//使用一个局部作用域变量,以避免每次都重写上一个实例。
//如果以后需要访问它们,可以将它们推送到阵列上
弗吉尼亚州