关于Flex 初始化的research

后来研究发现,不是取不到,而是在createChildren的时候,自定义的objcet还没有被赋值,只有当该组件的init事件之后才会被赋值,代码如下:
APP:


代码如下:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
    layout="absolute" xmlns:com="com.*">
    <mx:Script>
        <!--[CDATA[
            [Bindable]
            private var o:Object = {};
        ]]-->
    </mx:Script>
    <com:CustomPanel width="100%" height="100%" label="FlexInitResearch">
        <com:CustomButton label="Button"
            customString="customString test" customObject="{o}"/>
    </com:CustomPanel>
</mx:Application>

CustomPanel:


代码如下:

package com
{
    import mx.containers.Panel;
    import mx.events.FlexEvent;

public class CustomPanel extends Panel
    {
        public function CustomPanel()
        {
            super();
            this.addEventListener(FlexEvent.PREINITIALIZE,             onPreInit);
            this.addEventListener(FlexEvent.INITIALIZE,             onInit);
            this.addEventListener(FlexEvent.CREATION_COMPLETE,         onCreationComplete);
            this.addEventListener(FlexEvent.APPLICATION_COMPLETE,     onAppInitComplete);
        }

//=================================
        // event handler
        //=================================

private function onPreInit(event:FlexEvent):void
        {
            trace("CustomPanel[ PreInit ]");
        }

private function onInit(event:FlexEvent):void
        {
            trace("CustomPanel[ Init ]");
        }

private function onCreationComplete(event:FlexEvent):void
        {
            trace("CustomPanel[ CreationComplete ]");
        }

private function onAppInitComplete(event:FlexEvent):void
        {
            trace("CustomPanel[ AppInitComplete ]");
        }

//=================================
        // override function
        //=================================

override protected function createChildren():void
        {
            trace("CustomPanel[ Begin to createChildren ]");
            super.createChildren();
            trace("CustomPanel[ The end of createChildren ]");
        }

override protected function measure():void
        {
            trace("CustomPanel[ Begin to measure ]");
            super.measure();
            trace("CustomPanel[ The end of measure ]");
        }

override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
        {
            trace("CustomPanel[ Begin to updateDisplayList ]");
            super.updateDisplayList(unscaledWidth, unscaledHeight);
            trace("CustomPanel[ The end of updateDisplayList ]");
        }

override protected function layoutChrome(unscaledWidth:Number, unscaledHeight:Number):void
        {
            trace("CustomPanel[ Begin to layoutChrome ]");
            super.layoutChrome(unscaledWidth, unscaledHeight);
            trace("CustomPanel[ The end of layoutChrome ]");
        }

override protected function commitProperties():void
        {
            trace("CustomButton[ Begin to commitProperties ]");
            super.commitProperties();
            trace("CustomButton[ The end of commitProperties ]");
        }
    }
}

CustomButton:


代码如下:

package com
{
    import mx.controls.Button;
    import mx.events.FlexEvent;

public class CustomButton extends Button
    {
        //=================================
        // properties
        //=================================
        private var _customString:String     = "";
        private var _customObject:Object     = null;

public function get customString():String
        {
            return _customString;
        }
        //string
        public function set customString(value:String):void
        {
            trace("CustomButton( set customString )");
            _customString = value;
        }

//object
        public function get customObject():Object
        {
            return _customObject;
        }

public function set customObject(value:Object):void
        {
            trace("CustomButton( set customObject )");
            _customObject = value;
        }

//=================================
        // Constructor
        //=================================

public function CustomButton()
        {
            trace("CustomButton( Begin to Constructor )");
            super();
            this.addEventListener(FlexEvent.PREINITIALIZE,             onPreInit);
            this.addEventListener(FlexEvent.INITIALIZE,             onInit);
            this.addEventListener(FlexEvent.CREATION_COMPLETE,         onCreationComplete);
            this.addEventListener(FlexEvent.APPLICATION_COMPLETE,     onAppInitComplete);
            trace("CustomButton( The end of Constructor )");
        }

//=================================
        // event handler
        //=================================

private function onPreInit(event:FlexEvent):void
        {
            trace("CustomButton( PreInit )");
        }

private function onInit(event:FlexEvent):void
        {
            trace("CustomButton( Init )");
        }

private function onCreationComplete(event:FlexEvent):void
        {
            trace("CustomButton( Creation Complete )");
        }

private function onAppInitComplete(event:FlexEvent):void
        {
            trace("CustomButton( AppInitComplete )");
        }

//=================================
        // override function
        //=================================

override protected function createChildren():void
        {
            trace("CustomButton( Begin to createChildren )");
            super.createChildren();
            trace("CustomButton( The end of createChildren )");
        }

override protected function measure():void
        {
            trace("CustomButton( Begin to measure )");
            super.measure();
            trace("CustomButton( The end of measure )");
        }

override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
        {
            trace("CustomButton( Begin to updateDisplayList )");
            super.updateDisplayList(unscaledWidth, unscaledHeight);
            trace("CustomButton( The end of updateDisplayList )");
        }

override protected function commitProperties():void
        {
            trace("CustomButton( Begin to commitProperties )");
            super.commitProperties();
            trace("CustomButton( The end of commitProperties )");
        }
    }
}

最后运行的结果是:
CustomPanel[ PreInit ]
CustomPanel[ Begin to createChildren ]
CustomButton( Begin to Constructor )
CustomButton( The end of Constructor )
CustomButton( set customString ) //基本变量(String,Number(int,uint),Boolean)在PreInit 之前就被赋值
CustomButton( PreInit )
CustomButton( Begin to createChildren )
CustomButton( The end of createChildren )
CustomButton( Init )
CustomButton( set customObject ) //自定义对象变量在 Init之后才能被赋值,所以在createChildren中取不到
CustomPanel[ Init ] //有子控件的时候,Init 事件是在createChildren中发出的
CustomPanel[ The end of createChildren ]
CustomButton( set customObject )
CustomButton[ Begin to commitProperties ]
CustomButton[ The end of commitProperties ]
CustomButton( Begin to commitProperties )
CustomButton( The end of commitProperties )
CustomButton( Begin to measure )
CustomButton( The end of measure )
CustomPanel[ Begin to measure ]
CustomPanel[ The end of measure ]
CustomPanel[ Begin to updateDisplayList ]
CustomPanel[ Begin to layoutChrome ]
CustomPanel[ The end of layoutChrome ] //证明layoutChrome是在updateDisplayList 中被调用的
CustomPanel[ The end of updateDisplayList ]
CustomButton( Begin to updateDisplayList )
CustomButton( The end of updateDisplayList )
CustomButton( Creation Complete )
CustomPanel[ CreationComplete ]
后来又发现,在MXML中设置基本变量和对象变量有一定区别,那就是对象变量必须要用大括号{}包起来,于是就想,会不会是绑定造成的,将APP改成如下,发现跟预想中的一样,最后的输出结果与上面的一样:


代码如下:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
    layout="absolute" xmlns:com="com.*">
    <mx:Script>
        <!--[CDATA[
            [Bindable]
            private var o:String = "String test";//将原对象换成字符串
        ]]-->
    </mx:Script>
    <com:CustomPanel width="100%" height="100%" label="FlexInitResearch">
        <com:CustomButton label="Button"
            customString="customString test" customObject="{o}"/>
    </com:CustomPanel>
</mx:Application>

为了进一步确定是由于绑定造成的赋值时期不一致,我又做了如下的一个试验,不使用绑定给对象变量赋值:


代码如下:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
    layout="absolute" xmlns:com="com.*">
    <com:CustomPanel width="100%" height="100%" label="FlexInitResearch">
        <com:CustomButton label="Button"
            customString="customString test">
            <com:customObject>
                <mx:ArrayCollection/>
            </com:customObject>
        </com:CustomButton>
    </com:CustomPanel>
</mx:Application>

其结果为:
CustomPanel[ PreInit ]
CustomPanel[ Begin to createChildren ]
CustomButton( Begin to Constructor )
CustomButton( The end of Constructor )
CustomButton( set customString )
CustomButton( set customObject ) //赋值时间与基本变量相同
CustomButton( PreInit )
CustomButton( Begin to createChildren )
CustomButton( The end of createChildren )
CustomButton( Init )
CustomPanel[ Init ]
CustomPanel[ The end of createChildren ]
CustomButton[ Begin to commitProperties ]
CustomButton[ The end of commitProperties ]
CustomButton( Begin to commitProperties )
CustomButton( The end of commitProperties )
CustomButton( Begin to measure )
CustomButton( The end of measure )
CustomPanel[ Begin to measure ]
CustomPanel[ The end of measure ]
CustomPanel[ Begin to updateDisplayList ]
CustomPanel[ Begin to layoutChrome ]
CustomPanel[ The end of layoutChrome ]
CustomPanel[ The end of updateDisplayList ]
CustomButton( Begin to updateDisplayList )
CustomButton( The end of updateDisplayList )
CustomButton( Creation Complete )
CustomPanel[ CreationComplete ]
问题确定,所以以后再createChildren 中使用自定义对象变量的时候必须要注意,否则就会出现空指针之类的问题了。

(0)

相关推荐

  • 关于Flex 初始化的research

    后来研究发现,不是取不到,而是在createChildren的时候,自定义的objcet还没有被赋值,只有当该组件的init事件之后才会被赋值,代码如下:APP: 复制代码 代码如下: <?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"     layout="absolute&

  • Flex中怎么给表格中的滚动条定位避免刷新回到原处

    1.问题背景 如果表格中的字段过多,会出现滚动条,在将滚动条滚到一定的位置时,重新刷新表格,滚动条会回到原处,原来查看的字段还得继续滚动,才能查看到. 2.实现实例 <?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/f

  • Flex设置LinkButton的背景色有思路有源码

    1.设计思路 由于Flex中没有设置LinkButton的背景色的属性,现在得从两个方面入手:第一,直接通过调用样式方法画出LinkButton的背景色:第二,设置LinkButton的背景图片.这里,讲述的是第一种方法 2.设计源码 <?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xml

  • Flex播放器(实现播放、缓冲进度条和音频曲线显示)

    一时兴起,玩起了Flex,本来还想要做个Flex博客,不过目前还只能在里面树个公告...没办法做完啊,河蟹的个杯具的!Flex布局不像是CSS,精美Flash动画不是拖一个两个控件就能做出来滴,而是一笔一条线绘制出来滴!这些我都还不熟悉,所有折腾快一个星期了,每天都是搞到头大才睡觉,今天终于能出一个简单的播放器. 一直很喜欢音乐这个东西,喜欢Jay,更喜欢他的歌,也很崇拜小猪,他的一段灰色空间曾让我激流奋进,想过自己能做个播客放自己喜欢听的歌曲,出于自恋那样会更有一点点满足感.呃~走神了,前二天

  • Flex中Array的IndexOf 的作用示例介绍

    Flex中 Array 的IndexOf 的作用 1.说明 indexOf用于在索引中从小到大查找,如果查得到就返回索引值,查不到就返回-1: 2.实例 (1)设计源码 复制代码 代码如下: <?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://n

  • Flex中TitleWindow传值思路及实现

    1.设计思路 (1)新建一个DataGrid,在其中最后一列加入三个按钮:新增.修改和删除: (2)点击新增按钮,可以将表格新增一行: (3)单击"修改"按钮,可以修改表格中该行的一些属性: (4)单击"删除"按钮,会将表格中该行删除. 2.实现步骤 (1)新建一个应用程序,DataGrid.mxml DataGrid.mxml: 复制代码 代码如下: <?xml version="1.0" encoding="utf-8&quo

  • Flex父子窗口相互调用实现思路及源码

    1.设计思路 (1)子窗口调用父窗口的方法 (2)子窗口做了修改后,返回父窗口,父窗口调用子窗口函数 2.设计源码 (1)父窗口 ParentWindow.mxml: 复制代码 代码如下: <?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns

  • Flex文件读取报错示例

    Flex文件读取 1.s:WindowedApplication 复制代码 代码如下: <?xml version="1.0" encoding="utf-8"?> <s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="

  • Flex中TabNavigator设置Tabs样式思路及源码

    1.设计思路 (1)设计一个TabNavigator,其中包含两个Tabs: (2)设置Tabs样式 2.设计源码 Tabs.mxml: 复制代码 代码如下: <?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex

  • Flex实现的上传摄像头拍照并将UI保存为图片

    flex客户端代码: 复制代码 代码如下: <?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx

随机推荐