当前位置: 代码迷 >> Web前端 >> 经过SWFLoader加载Application并进行父子组件交互
  详细解决方案

经过SWFLoader加载Application并进行父子组件交互

热度:110   发布时间:2012-09-03 09:48:39.0
通过SWFLoader加载Application并进行父子组件交互

MainApp.mxml:

?

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" fontSize="18"
? horizontalAlign="center">
?<mx:Script>
??<![CDATA[
???import mx.controls.Alert;
???import mx.managers.SystemManager;
???private function get subApp():SubApp{
????if (loader.content==null) return null;
????//loader.content指向子组件的SystemManager,通过.application即可获取子组件的application对象
????return SubApp(SystemManager(loader.content).application);
???}
???private function changeSubAppContent1():void{
????if (subApp==null){
?????Alert("子应用加载未完成");
?????return ;
????}
????subApp.changeText("这是通过方法修改过的内容!");
???}
???private function changeSubAppContent2():void{
????if (subApp==null){
?????Alert("子应用加载未完成");
?????return ;
????}
????subApp['label2'].text="这是通过属性修改过的内容!";
???}???
??]]>
?</mx:Script>
?<mx:VBox width="100%" height="100%">
??<mx:VBox height="150" width="100%">
???<mx:Button? label="通过方法修改子应用内容" click="changeSubAppContent1();"/>?
???<mx:Button? label="通过属性修改子应用内容" click="changeSubAppContent2();"/>?
???<mx:Label id="mainLabel" text="欢迎修改内容!"/>
??</mx:VBox>??
??<mx:SWFLoader height="100%" width="100%" id="loader"
???source="SubApp.swf" showBusyCursor="true">???
??</mx:SWFLoader>?
?</mx:VBox>
</mx:Application>

?

SubApp.mxml:

?

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
?fontSize="18" fontWeight="bold" width="410" height="200">
?<mx:Script>
??<![CDATA[
???public function changeText(text:String):void{
????label1.text=text;
???}
???private function changeParentInfo():void{
?????????? //Application.application可获取父组件应用对象
????Application.application['mainLabel'].text="这是子应用修改的!";
???}
??]]>
?</mx:Script>
?<mx:Panel width="400" headerColors="200" height="148">
??<mx:Label id="label1"? text="这是我原始显示的内容:我的名字label1"/>?
??<mx:Label id="label2"? text="这是我原始显示的内容:我的名字label2"/>?
??<mx:Button label="修改父应用信息" click="changeParentInfo();"/>
?</mx:Panel>
</mx:Application>

  相关解决方案