0
点赞
收藏
分享

微信扫一扫

FLEX实践—模拟Application与Module间的事件监听


      一直找到如何在Application中发出一个事件,让Module捕获该事件并做出相应的动作。但是一直没找到,于是模拟了一个这样的情景。

 

WatchTest.mxml
 
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
 import mx.binding.utils.ChangeWatcher;
  import mx.controls.Alert;  //设置一个绑定变量,模拟Application的事件
  [Bindable]public var monitor:String="AA";
  private function changeMonitor():void{
   monitor="BB";
  }
]]>
</mx:Script>
 <mx:Label x="419" y="97" text="{this.monitor}" width="157" height="23" id="watchTest"/>
 <mx:Button x="603" y="95" label="Click me" click="changeMonitor()"/>
 <mx:ModuleLoader x="286" y="160" width="596" height="315" url="Module/WatchModule.swf">
 </mx:ModuleLoader> 
</mx:Application> 
WatchModule.mxml
 
<?xml version="1.0" encoding="utf-8"?>
<mx:Module xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="400" 
 height="300" backgroundColor="#FBDADA" creationComplete="init()">
    <mx:Script>
      <![CDATA[
       import mx.binding.utils.ChangeWatcher;
        import mx.controls.Alert;
        var watcher:ChangeWatcher;        private function init():void{
        //设置一个观察器观察monitorText的值,一旦变化,立即触发valueChanged方法
         watcher=ChangeWatcher.watch(monitorText,"text",valueChanged);
        }
        private function valueChanged(event:Event):void{
         Alert.show("Application's monitor has changed!");
        }
      ]]>
    </mx:Script><!--绑定Application的monitor变量,注意使用parentApplication.monitor获取值-->
 <mx:Label x="120" y="62" text="{this.parentApplication.monitor}" width="135" height="22" id="monitorText"/>
 
</mx:Module>

 

 

举报

相关推荐

0 条评论