Actionscript 3 在Flex 4.5中使用ModuleLoader
我想在Flex应用程序中加载模块时添加一个进度条。因此,我创建了一个模块加载器,如下所示。但是我不知道如何在我的应用程序中使用它来加载模块。谁能帮我一下吗。我的代码如下。我的主应用程序有一个viewstack,其中包含以下两个模块。请注意,Module1是一个登录表单,当我在登录表单上单击OK时,我必须加载Module1Actionscript 3 在Flex 4.5中使用ModuleLoader,actionscript-3,apache-flex,flex4.5,Actionscript 3,Apache Flex,Flex4.5,我想在Flex应用程序中加载模块时添加一个进度条。因此,我创建了一个模块加载器,如下所示。但是我不知道如何在我的应用程序中使用它来加载模块。谁能帮我一下吗。我的代码如下。我的主应用程序有一个viewstack,其中包含以下两个模块。请注意,Module1是一个登录表单,当我在登录表单上单击OK时,我必须加载Module1 <mx:ViewStack id="mainstack" width="100%" height="100%"> <mx:HBox id="
<mx:ViewStack id="mainstack" width="100%" height="100%">
<mx:HBox id="Mod1Loader" width="100%" height="100%" label="Mod1Loader" horizontalAlign="center" verticalAlign="middle">
<mx:ModuleLoader url="Mod1.swf" id="Module1" />
</mx:HBox>
<mx:HBox id="Mod2Loader" width="100%" height="100%" label="Mod2Loader" horizontalAlign="center" verticalAlign="middle">
<mx:ModuleLoader url="Mod2.swf" id="Module2" width="100%" height="100%"/>
</mx:HBox>
</mx:ViewStack>
CustomModuleLoader.mxml如下所示:
<?xml version="1.0" encoding="utf-8"?>
<s:ModuleLoader xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
width="400" height="300"
creationComplete="init()">
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Script>
<![CDATA[
import mx.core.UIComponent;
public var standin:UIComponent;
public function init():void {
addEventListener("urlChanged", onUrlChanged);
addEventListener("loading", onLoading);
addEventListener("progress", onProgress);
addEventListener("setup", onSetup);
addEventListener("ready", onReady);
addEventListener("error", onError);
addEventListener("unload", onUnload);
standin = panel;
removeElement(standin);
}
public function onUrlChanged(event:Event):void {
if (url == null) {
if (contains(standin))
removeElement(standin);
} else {
if (!contains(standin))
addElement(standin);
}
progress.indeterminate=true;
}
public function onLoading(event:Event):void {
progress.label="Loading module " + url;
if (!contains(standin))
addElement(standin);
progress.indeterminate=true;
}
public function onProgress(event:Event):void {
progress.label="Loaded %1 of %2 bytes...";
progress.indeterminate=false;
}
public function onSetup(event:Event):void {
progress.label="Module " + url + " initialized!";
progress.indeterminate=false;
}
public function onReady(event:Event):void {
progress.label="Module " + url + " successfully loaded!";
if (contains(standin))
removeElement(standin);
}
public function onError(event:Event):void {
progress.label="Error loading module " + url;
}
public function onUnload(event:Event):void {
if (url == null) {
if (contains(standin))
removeElement(standin);
} else {
if (!contains(standin))
addElement(standin);
}
progress.indeterminate=true;
progress.label="Module " + url + " was unloaded!";
}
]]>
</fx:Script>
<s:Panel id="panel" width="100%" title="Status of Operations">
<s:layout>
<s:VerticalLayout/>
</s:layout>
<mx:ProgressBar width="100%" id="progress" source="{this}"/>
</s:Panel>
</s:ModuleLoader>
你需要宝贵的帮助
非常感谢。正如文档中所述,您可以直接使用progressbar绑定模块的加载: 您必须将progressbar的
源设置为moduleloader,并定义标签
。将模式
设置为轮询,一切都将自动工作
--编辑
事实上,由于某些原因,adobe文档不同步,因为ModuleLoader没有这些属性
一个简单的解决方案是添加所需的属性:
[Bindable]
public var bytesLoaded:Number = 0;
[Bindable]
public var bytesTotal:Number = 100;
然后更改onProgress函数以更新属性:
public function onProgress(event:ProgressEvent):void {
// update the attributes
bytesLoaded = event.bytesLoaded;
bytesTotal = event.bytesTotal;
progress.label="Loaded %1 of %2 bytes...";
progress.indeterminate=false;
}
当我在本地环境中测试时,我看不到进度条,但至少我没有任何问题
M.我按照你说的做了,但是我得到了错误:ReferenceError:error#1069:Property bytesLoaded在com.teotys.F500.menus.CustomModuleLoader上找不到,并且没有默认值。在flash的mx.controls::ProgressBar/updatePolledHandler()[E:\dev\4.5.1\frameworks\projects\mx\src\mx\controls\ProgressBar.as:1822]上。在flash的utils::Timer/_timerDispatch()上。utils::Timer/tick()知道如何解决这个问题吗?谢谢,adobe的帮助似乎与现实不同步。。。然后可以手动执行此操作:很遗憾,将此行添加到progressBar并不能解决此问题。我仍然得到相同的错误..代码中有我丢失的信息吗?代码现在看起来很好。也没有错误..但是现在没有模块加载。我的浏览器加载了一个空白的白页。通过更新我的答案中的代码(忽略注释中的代码),我成功加载了我的模块,一切正常。