关于Flex 初始化的research

所属分类: 网页制作 / Flash 阅读数: 1229
收藏 0 赞 0 分享
后来研究发现,不是取不到,而是在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 中使用自定义对象变量的时候必须要注意,否则就会出现空指针之类的问题了。
更多精彩内容其他人还在看

利用asp或jsp,flash怎样把数据库中的一张表中的所有记录读取并显示出来

利用asp或jsp,flash怎样把数据库中的一张表中的所有记录读取并显示出来
收藏 0 赞 0 分享

Flash 中"设为首页"代码

Flash 中"设为首页"代码
收藏 0 赞 0 分享

FLEX TitleWindow之间数据传输的示例

在Flex里,一般的弹出窗口(除了Alert以外)都可以用TitleWindow组件完成,主窗口和TitleWindow的数据传输可以用以下方法
收藏 0 赞 0 分享

FLEX 仿Google联想框效果

FLEX--仿Google联想框效果--现在好多地方都用到了这样的效果,当然,现在Google下线了,不让用了,但是跟网上说的一样,想办法,它还是能出来的
收藏 0 赞 0 分享

FLEX 日期时间操作

找了很久,还是没有找到包含时间以下的东西,算了,还是麻烦一点,用日期加时间来处理吧.这里需要注意的是,它的时间日期跟其他语言是差不多的.所以要注意进行处理.
收藏 0 赞 0 分享

Flex 自动获取焦点 监听全局键盘事件

Flex 编程注意之自动获取焦点、监听全局键盘事件
收藏 0 赞 0 分享

Flex 编程注意之性能优化、垃圾回收的一些总结

自从开始做Flex、ActionScript 3.0的项目,我就一直与垃圾回收、性能优化这些问题打交道,因此也总结了一些优化的方案,同时在一些QQ群中也得到了一些“高人”的指点,因此将此内容记录一下。
收藏 0 赞 0 分享

Flex CategoryAxis 字体样式修改

Flex编程注意之修改CategoryAxis的字体样式(大小、颜色、粗斜体等)
收藏 0 赞 0 分享

Flex 编程注意之Flex Complier参数

由于上一篇文章《Flex编程注意之Namespace的用法》引出了Flex编译时自带的两个参数:-namespace -include-namespace,正好想到可以写一篇关于Flex Complier时的一些自定义参数。
收藏 0 赞 0 分享

Flex Namespace的用法

自定义自己组件的namespace呢 去Adobe那边的compiler参数查找了一下,发现可以利用-namespace、-include-namespaces这两个参数来指定自己的URL。
收藏 0 赞 0 分享
查看更多