关于Flex 初始化的research

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

Flex 创建一个自定义风格的HRule或VRule

Flex中如何通过strokeWidth, strokeColor和shadowColor样式,创建一个自定义风格的HRule或VRule
收藏 0 赞 0 分享

Flex Gumbo 通过textJustify样式设置TextBox文字对齐的例子

接下来的例子演示了Flex Gumbo中如何通过textJustify样式,设置TextBox文字对齐。
收藏 0 赞 0 分享

Flex Gumbo 通过smooth属性设置BitmapGraphic对象平滑度的例子

接下来的例子演示了Flex Gumbo中如何通过smooth属性,设置BitmapGraphic对象平滑度。
收藏 0 赞 0 分享

Flex Javascript交互实现代码

删除swf这里需要提醒下,因为embedSWF是替换标签,而不是填充。
收藏 0 赞 0 分享

Flex 动态绑定BindingUtils.bindProperty

Flex 动态绑定BindingUtils.bindProperty实现代码。
收藏 0 赞 0 分享

解决Ajax悬停效果,无法遮蔽FLASH的问题

但遇到的问题是:很多的页面中有FALSH导航栏,或者FLASH广告动画等,这些FLASH是不会自动被CSS样式遮蔽的.怎么办呢?
收藏 0 赞 0 分享

Flex 对象持久化

总有人问我关于Flex对象序列化和持久化的问题,很多人认为对象不能拷贝到服务器,其实浅度的拷贝是可以做到的。
收藏 0 赞 0 分享

XML TO ArrayCollection 两种实现方式

最近研究FLEX,在网上看过100遍同一篇文章,结果测试不通,不知道到底能不能跑通,最后翻资料自己试验处理。
收藏 0 赞 0 分享

java TO ArrayCollection

因为new ArrayCollection 需要 array对象,event.result是空间返回对象(JAVA方法返回值)所以类型不同无法使用new 构造ArrayCollection ,可是恶心的FLEX偏偏支持javalist=ArrayCollection
收藏 0 赞 0 分享

Flex DataGrid DataGridColumn数据颜色多样化-类型替换

用得多了,发觉自己了解的真的是九牛之一毛都没有,最近用到了从后台读出数据时显示的问题,相信很多人都有用整形数据来代替字符串数据的情况
收藏 0 赞 0 分享
查看更多