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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值