首页 > 其他分享 >Flex的事件驱动问题

Flex的事件驱动问题

时间:2023-07-20 16:03:41浏览次数:38  
标签:Flex gt courier quot 事件驱动 lt amp EventFlow0 问题

说,Flex一切都是事件,flex是事件驱动的。(Flex is an event driven programming model, everything (and I mean everything) happens due to an event. )。


这个问题的起源是我写了一个读取VBox高度的代码:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" backgroundColor="#99ccff" initialize="init()">
<mx:Style>
</mx:Style>
<mx:Script>
	<!--[CDATA[
		import mx.collections.XMLListCollection;
		import mx.controls.List;
		 import mx.collections.ArrayCollection;
         [Bindable]
		 public var left:XML=
			<list>
                <info label="モーニング娘。">
                	</info>
              </list>;
		[Bindable]
		private var leftList:XMLListCollection= new XMLListCollection(left.info);
	
		// init the whole app.
		 private function init():void
		 {
		 	leftTree.dataProvider=leftList;	
		 	leftTree.selectedIndex=0;
		 	
		 	//srightVBox.backgroundImage="http://www.lovezaoan.cn/attachment/200905/24/25_1243179290Yp2p.jpg"; 	
		 	
		 	test.text = leftCanvas.width+ " "+hdBox.height;
		 }
		 
		 // init the right when 1st level nodes are selected in the left
		 private function initRight():void{
		 	
		 }
	]]-->
</mx:Script>
	<mx:HDividedBox width="100%" height="100%" id="hdBox">
	<mx:Canvas height="100%" width="200" id="leftCanvas">
	
	<mx:Tree width="100%" height="100%" backgroundColor="#ff6ac8" id="leftTree"  labelField="@label" fontSize="14" top="0" left="0" >
			        
		</mx:Tree>
		<mx:Panel height="60" headerHeight="10" bottom="0" width="100%" left="0">
		<mx:HBox>
			
		
			<mx:Image source="../resource/logo.gif">
			</mx:Image>
			<mx:Label text="爱早安出品" fontSize="20" fontWeight="bold">
				
			</mx:Label>
			</mx:HBox>
		</mx:Panel>
	</mx:Canvas>
		<mx:VBox id="rightVBox" height="100%" backgroundImage="@Embed(source='../resource/mm.jpg')" backgroundSize="100%"> 
			<mx:Text id="test">
				
			</mx:Text>
		</mx:VBox>
	</mx:HDividedBox>
</mx:Application>



// init the whole app.
          private function init():void
          {
              leftTree.dataProvider=leftList;    
              leftTree.selectedIndex=0;
              
              //srightVBox.backgroundImage="http://www.lovezaoan.cn/attachment/200905/24/25_1243179290Yp2p.jpg";     
              
              test.text = leftCanvas.width+ " "+hdBox.height; 
          }上面的粗体是读leftCavas和hdBox的大小,但是得到的是0.

根据上面的文章,得到的0的原因是没有理解flex中的组件加载顺序。根据上面文章提供的例子。
http://www.onflex.org/flexapps/applications/EventFlow/srcview/index.html 
<?xml version="1.0" encoding="utf-8"?>
<mx:Application 
    xmlns:mx="http://www.adobe.com/2006/mxml" 
    layout="absolute"
    backgroundGradientColors="[#67cbff, #fcffff]" 
    color="#000000" 
    fontSize="12"    
    preinitialize="report( event , 'preinitialize' )"
    initialize="report( event , 'initialize' )"
    creationComplete="report( event , 'creationComplete' )"
    applicationComplete="report( event , 'applicationComplete' )"
    >
    
    <mx:Script>
        <!--[CDATA[    
                    
            [Bindable]
            
            public var outTextData:String="";
            
            public function report( event:Event , value:String ):void
            {
                outTextData += String( flash.utils.getTimer() ) + 'ms >> ' 
                + event.currentTarget + '.' + value + '/n';    
            }
            
        ]]-->
    </mx:Script>
    
    <mx:TextArea
        id="outTextArea"
        text="{ outTextData }"
        right="10" left="10" top="50" bottom="10" alpha="0.5"
        wordWrap="false"
        initialize="report( event , 'initialize' )"
        creationComplete="report( event , 'creationComplete' )" 
        />
    
    <mx:Button 
        y="10" height="30" left="168" width="150"
        id="HelloButton"
        label="Say Hello"
        initialize="report( event , 'initialize' )"
        creationComplete="report( event , 'creationComplete' )" 
        rollOver="report( event , 'rollOver' )" 
        rollOut="report( event , 'rollOut' )" 
        click="report( event , 'click > Hello!' )" 
        />
        
    <mx:Button
        id="GoodByeButton" 
        label="Say Goodbye" 
        y="10" left="10" height="30" width="150" color="#000000"
        initialize="report( event , 'initialize' )"
        creationComplete="report( event , 'creationComplete' )" 
        click="report( event , 'click > Goodbye!' )" 
        />
        
    <mx:Button
        id="ClearButton" 
        label="Clear" 
        y="10" left="326" height="30" color="#000000" right="10"        
        initialize="report( event , 'initialize' )"
        creationComplete="report( event , 'creationComplete' )" 
        click="outTextData='';report( event , 'click' )" 
         />
    
</mx:Application>

结果:
http://www.onflex.org/flexapps/applications/EventFlow/

  
 
  &amp;amp;amp;amp;amp;amp;amp;lt;span style=&amp;amp;amp;amp;amp;amp;amp;quot;font-family: courier new,courier;&amp;amp;amp;amp;amp;amp;amp;quot; mce_style=&amp;amp;amp;amp;amp;amp;amp;quot;font-family: courier new,courier;&amp;amp;amp;amp;amp;amp;amp;quot;&amp;amp;amp;amp;amp;amp;amp;gt; &amp;amp;amp;amp;amp;amp;amp;amp;amp;lt;object classid=&amp;amp;amp;amp;amp;amp;amp;amp;amp;quot;clsid:D27CDB6E-AE6D-11cf-96B8-444553540000&amp;amp;amp;amp;amp;amp;amp;amp;amp;quot; id=&amp;amp;amp;amp;amp;amp;amp;amp;amp;quot;EventFlow&amp;amp;amp;amp;amp;amp;amp;amp;amp;quot; width=&amp;amp;amp;amp;amp;amp;amp;amp;amp;quot;100%&amp;amp;amp;amp;amp;amp;amp;amp;amp;quot; height=&amp;amp;amp;amp;amp;amp;amp;amp;amp;quot;100%&amp;amp;amp;amp;amp;amp;amp;amp;amp;quot; codebase=&amp;amp;amp;amp;amp;amp;amp;amp;amp;quot;http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab&amp;amp;amp;amp;amp;amp;amp;amp;amp;quot;&amp;amp;amp;amp;amp;amp;amp;amp;amp;gt; &amp;amp;amp;amp;amp;amp;amp;amp;amp;lt;param name=&amp;amp;amp;amp;amp;amp;amp;amp;amp;quot;movie&amp;amp;amp;amp;amp;amp;amp;amp;amp;quot; value=&amp;amp;amp;amp;amp;amp;amp;amp;amp;quot;EventFlow.swf&amp;amp;amp;amp;amp;amp;amp;amp;amp;quot; /&amp;amp;amp;amp;amp;amp;amp;amp;amp;gt; &amp;amp;amp;amp;amp;amp;amp;amp;amp;lt;param name=&amp;amp;amp;amp;amp;amp;amp;amp;amp;quot;quality&amp;amp;amp;amp;amp;amp;amp;amp;amp;quot; value=&amp;amp;amp;amp;amp;amp;amp;amp;amp;quot;high&amp;amp;amp;amp;amp;amp;amp;amp;amp;quot; /&amp;amp;amp;amp;amp;amp;amp;amp;amp;gt; &amp;amp;amp;amp;amp;amp;amp;amp;amp;lt;param name=&amp;amp;amp;amp;amp;amp;amp;amp;amp;quot;bgcolor&amp;amp;amp;amp;amp;amp;amp;amp;amp;quot; value=&amp;amp;amp;amp;amp;amp;amp;amp;amp;quot;#67cbff&amp;amp;amp;amp;amp;amp;amp;amp;amp;quot; /&amp;amp;amp;amp;amp;amp;amp;amp;amp;gt; &amp;amp;amp;amp;amp;amp;amp;amp;amp;lt;param name=&amp;amp;amp;amp;amp;amp;amp;amp;amp;quot;allowScriptAccess&amp;amp;amp;amp;amp;amp;amp;amp;amp;quot; value=&amp;amp;amp;amp;amp;amp;amp;amp;amp;quot;sameDomain&amp;amp;amp;amp;amp;amp;amp;amp;amp;quot; /&amp;amp;amp;amp;amp;amp;amp;amp;amp;gt; &amp;amp;amp;amp;amp;amp;amp;amp;amp;lt;embed src=&amp;amp;amp;amp;amp;amp;amp;amp;amp;quot;EventFlow.swf&amp;amp;amp;amp;amp;amp;amp;amp;amp;quot; mce_src=&amp;amp;amp;amp;amp;amp;amp;amp;amp;quot;EventFlow.swf&amp;amp;amp;amp;amp;amp;amp;amp;amp;quot; quality=&amp;amp;amp;amp;amp;amp;amp;amp;amp;quot;high&amp;amp;amp;amp;amp;amp;amp;amp;amp;quot; bgcolor=&amp;amp;amp;amp;amp;amp;amp;amp;amp;quot;#67cbff&amp;amp;amp;amp;amp;amp;amp;amp;amp;quot; width=&amp;amp;amp;amp;amp;amp;amp;amp;amp;quot;100%&amp;amp;amp;amp;amp;amp;amp;amp;amp;quot; height=&amp;amp;amp;amp;amp;amp;amp;amp;amp;quot;100%&amp;amp;amp;amp;amp;amp;amp;amp;amp;quot; name=&amp;amp;amp;amp;amp;amp;amp;amp;amp;quot;EventFlow&amp;amp;amp;amp;amp;amp;amp;amp;amp;quot; align=&amp;amp;amp;amp;amp;amp;amp;amp;amp;quot;middle&amp;amp;amp;amp;amp;amp;amp;amp;amp;quot; play=&amp;amp;amp;amp;amp;amp;amp;amp;amp;quot;true&amp;amp;amp;amp;amp;amp;amp;amp;amp;quot; loop=&amp;amp;amp;amp;amp;amp;amp;amp;amp;quot;false&amp;amp;amp;amp;amp;amp;amp;amp;amp;quot; quality=&amp;amp;amp;amp;amp;amp;amp;amp;amp;quot;high&amp;amp;amp;amp;amp;amp;amp;amp;amp;quot; allowScriptAccess=&amp;amp;amp;amp;amp;amp;amp;amp;amp;quot;sameDomain&amp;amp;amp;amp;amp;amp;amp;amp;amp;quot; type=&amp;amp;amp;amp;amp;amp;amp;amp;amp;quot;application/x-shockwave-flash&amp;amp;amp;amp;amp;amp;amp;amp;amp;quot; pluginspage=&amp;amp;amp;amp;amp;amp;amp;amp;amp;quot;http://www.adobe.com/go/getflashplayer&amp;amp;amp;amp;amp;amp;amp;amp;amp;quot;&amp;amp;amp;amp;amp;amp;amp;amp;amp;gt; &amp;amp;amp;amp;amp;amp;amp;amp;amp;lt;/embed&amp;amp;amp;amp;amp;amp;amp;amp;amp;gt;&amp;amp;amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;amp;amp;gt; 
 

  &amp;amp;amp;amp;amp;amp;amp;lt;span style=&amp;amp;amp;amp;amp;amp;amp;quot;font-family: courier new,courier;&amp;amp;amp;amp;amp;amp;amp;quot; mce_style=&amp;amp;amp;amp;amp;amp;amp;quot;font-family: courier new,courier;&amp;amp;amp;amp;amp;amp;amp;quot;&amp;amp;amp;amp;amp;amp;amp;gt;&amp;amp;amp;amp;amp;amp;amp;amp;lt;br /&amp;amp;amp;amp;amp;amp;amp;amp;gt;&amp;amp;amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;amp;amp;gt; 
 

  &amp;amp;amp;amp;amp;amp;amp;lt;span style=&amp;amp;amp;amp;amp;amp;amp;quot;font-family: courier new,courier;&amp;amp;amp;amp;amp;amp;amp;quot; mce_style=&amp;amp;amp;amp;amp;amp;amp;quot;font-family: courier new,courier;&amp;amp;amp;amp;amp;amp;amp;quot;&amp;amp;amp;amp;amp;amp;amp;gt;&amp;amp;amp;amp;amp;amp;amp;amp;lt;br /&amp;amp;amp;amp;amp;amp;amp;amp;gt;&amp;amp;amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;amp;amp;gt; 
 
 
167ms >> EventFlow0.preinitialize
 183ms >> EventFlow0.outTextArea.initialize
 187ms >> EventFlow0.HelloButton.initialize
 188ms >> EventFlow0.GoodByeButton.initialize
 189ms >> EventFlow0.ClearButton.initialize
 189ms >> EventFlow0.initialize
 243ms >> EventFlow0.outTextArea.creationComplete
 243ms >> EventFlow0.HelloButton.creationComplete
 243ms >> EventFlow0.GoodByeButton.creationComplete
 244ms >> EventFlow0.ClearButton.creationComplete
 244ms >> EventFlow0.creationComplete
 246ms >> EventFlow0.applicationComplete


从这个结果看出,组件的初始化过程是:

applicaiton先preinitialize,然后是各个组件依次initialize,然后是application的initialize,然后是各个组件依次creationComplete,然后是application的creationComplete,最后是applicatonComplete。


所以,我最初的代码里面把application的initialize理解成了html中body的onload了,所以在这个时候做一些对其他组件的初始化操作,包括获取leftCanvas和hdBox的大小的时候就不对了。以leftCanvas为例,这个时候它没有初始化完毕,所以其大小为0是正常的。

所以,代码要修改为:

creationComplete="init()"


即,不用applicaiton的initialize事件,而是creationComplete事件,因为这个时候,application下的组件都已经创建完毕了。这个时候它们有height和width的值了。


标签:Flex,gt,courier,quot,事件驱动,lt,amp,EventFlow0,问题
From: https://blog.51cto.com/u_11326739/6787346

相关文章

  • 存在则更新,不存在则插入的问题优化
    一、解决的场景   开发中,经常遇到这样的场景,数据库中存在记录,则需要更新这条记录,不存在这条记录,则插入这条记录   比如:给用户加积分,加道具,存在则直接字段加值,不存在这条记录需要插入初始化的一条数据;统计每天的参与数(每天生成一条记录)等等。二、优化过程1、先查......
  • 【网易云信】直播场景播放侧常见问题分析与实践经验
    常见的播放流程播放器主要流程分析 播放器的播放流程与推流过程类似,但是顺序相反。推流端先采集音频和视频,进行音视频编码和封装,并按照流媒体协议进行处理,最终得到输出流。而播放器则将输入流经过解析和解封装,得到音频包(如AAC)和视频包(如H.264、H.265),并进行解码以获取音频帧......
  • spring boot使用mongodb时,xxxRepository不能Autowired的问题
    默认情况下,当继承MongoRepository的CRUD在@SpringBootApplication的子包下时,xxxRepository是能够自动被扫描和创建代理的。但是如果不在默认路径下,就无法注入了,即使是扫描路径加到了@ComponentScan也一样。解决方法:在springboot启动类中添加@EnableMongoRepositories注解,标注mon......
  • python安装问题
    --使用pipinstall命令安装插件时出现如下提示:(命令python-mpipinstall-ihttps://pypi.tuna.tsinghua.edu.cn/simplegitlab==1.0.2)(环境:Windows10)UnicodeDecodeError:'gbk'codeccan'tdecodebyte0xa2inposition202:illegalmultibytesequence[e......
  • 英文fc6下的中文问题
     fc6安装时只安装了英文,为了加入中文显示的支持,在fc6的安装盘dvd中找到kde-i18n-Chinese-3.5.4-1.noarch.rpm和kde-i18n-Chinese-Big5-3.5.4-1.noarch.rpm,注意,其他的语言支持也是类似名称的包。中文输入问题:......
  • 搬运 -阮一峰的网络日志 --Flex 布局教程:实例篇
    原文链接:http://www.ruanyifeng.com/blog/2015/07/flex-examples.html语法: https://www.cnblogs.com/yuwen1995/p/17568483.html一、骰子的布局骰子的一面,最多可以放置9个点。下面,就来看看Flex如何实现,从1个点到9个点的布局。你可以到codepen查看Demo。如果不加说明,本节的......
  • BOSHIDA DC电源模块关于转换效率的问题
    BOSHIDADC电源模块关于转换效率的问题DC电源模块是电子系统中常用的电源供应器件。其作用是将交流电转换成稳定直流电,以供电子设备使用。DC电源模块在实际应用中,其效率是一个非常重要的指标。因为高效率的电源模块可以减少功耗,提高电源的使用寿命,降低散热需求等。DC电源模块转......
  • 岩土工程监测振弦采集仪与振弦传感器的兼容性问题
    岩土工程监测振弦采集仪与振弦传感器的兼容性问题岩土工程监测中,振弦采集仪和振弦传感器是不可或缺的两个部分。振弦传感器是用来测量振动和位移的,而振弦采集仪则是用来接收和处理这些数据的。然而,由于市场上的振弦采集仪和振弦传感器种类繁多,其兼容性问题也引起了广泛关注。 ......
  • Linux 下运行.NET 6 7 8 程序遇到的两个问题
    一、/lib64/libstdc++.so.6:version`GLIBCXX_3.4.21‘notfound的解决办法1.下载libstdc++.so.6.0.21文件注意区分x84_64和aarch64架构,下载对应的版本2.把libstdc++.so.6.0.21复制到/lib64/libstdc++.so.6.0.213.运行命令exportLD_PRELOAD="/lib64/libstdc++.s......
  • 【Java面试题】Spring是如何解决循环依赖问题?
    ......