Image Flex 4.6优化移动应用程序的视图外观ContentCache与覆盖数据

Image Flex 4.6优化移动应用程序的视图外观ContentCache与覆盖数据,image,apache-flex,mobile,flex4,flex4.5,Image,Apache Flex,Mobile,Flex4,Flex4.5,我在本文中读到,我不应该在creationComplete处理程序中初始化视图的外观。相反,我应该在重写的数据设置器中更改视图的外观 本文的章节是: 重写数据设置程序,而不是在creationComplete处理程序中使用绑定或初始化视图的外观 1-首先,我想知道我是否通过以下方式做到了这一点: //My code is loading a set of images and adding them in a View. //On creationComplete of the View I

我在本文中读到,我不应该在creationComplete处理程序中初始化视图的外观。相反,我应该在重写的数据设置器中更改视图的外观

本文的章节是:

重写数据设置程序,而不是在creationComplete处理程序中使用绑定或初始化视图的外观

1-首先,我想知道我是否通过以下方式做到了这一点:

//My code is loading a set of images and adding them in a View. 
//On creationComplete of the View I am adding the images in case this is the first time          
//the view is shown. In case the view has been already accessed I use the data:  

   protected function view1_creationCompleteHandler(event:FlexEvent):void
        {
            if(!data) //On first creation of the view I create the data object
            {
                data = new Object();
                data.imageArray = new Array(); //set an array that will cache my images.
                for(var i:int = 0; i<36;i++)
                {
                    var img:Image = new Image();
                    img.source = 'assets/0'+i.toString()+'.png';
                    container.addElement(img);
                    (data.imageArray as Array).push(img);//Override the data for next time!
                }
            }
            else//Next time use the save images
            {
                for(var ix:int = 0; ix<(data.imageArray as Array).length;ix++)
                {
                    container.addElement((data.imageArray as Array)[ix]);
                }
            }
        }
//我的代码正在加载一组图像并将它们添加到视图中。
//在视图的创建完成时,我正在添加图像,以防这是第一次
//该视图如图所示。如果视图已被访问,我将使用以下数据:
受保护的函数视图1\u creationCompleteHandler(事件:FlexEvent):无效
{
if(!data)//在第一次创建视图时,我创建了数据对象
{
数据=新对象();
data.imageArray=new Array();//设置一个将缓存我的图像的数组。

对于(var i:int=0;i两者都不是。建议的要点是不要在creationComplete之后更改显示列表,这需要额外的更新周期。相反,当您在堆栈上推送视图时,应该在setter中启动更改。使用ContentCache与此无关(如果使用不当,有时会导致额外的开销)

在我看来

<s:View xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" title="MyView">

    <fx:Script>
        <![CDATA[
            import spark.components.Image;
            import spark.core.ContentCache;

            override protected function createChildren():void
            {
                super.createChildren();

                //you might want to do a sanity first check to make sure the 
                //data was passed in correctly...

                var cache:ContentCache = ContentCache(this.data.cache);

                for(var i:int = 0; i < 36; i++)
                {
                    var img:Image = new Image();
                    img.contentLoader = cache;
                    img.source = 'assets/0' + i.toString() + '.png';
                    container.addElement(img);
                }
            }
        ]]>
    </fx:Script>

    <s:VGroup id="container" />

</s:View>


Clear as water,非常感谢。问题是容器尚未创建。因此,现在的问题是如何将组件添加到容器的创建链中,因此他在创建时添加这些组件,而不是进行新的更新周期。我的理解是,MXML子代的创建在initia之后进行在为数据驱动容器设置数据属性之前。ViewStack不是这样吗?如果是这样,您可以覆盖createChildren并在那里第一次初始化数据,如果容器为null,则在数据设置器中提前返回。确定。覆盖数据肯定在createChildren之前。您可以id使用ContentCache可能会导致额外的开销。如果我在添加的图像上使用它呢?那么它会导致开销吗?正是这样!我保存ContentCache而不是将图像保存在数据上!它非常完美!平滑效果来自于在ContentCache上使用enableQueueing=true!我看到您我们也对我的问题发表了评论:你知道答案吗?我也有一种情况,我想用组件覆盖数据。我想知道是否有一种方法可以用这样一个简单的解决方案来实现。我是否应该就此提出一个问题。然后我会在这里评论问题的链接并删除这些评论。Just,因为无法直接向用户提问。如果你知道答案,你会感兴趣吗?再次感谢。
override public function set data(value:Object):void
{
    super.data = value;

    //this was poorly optimized, so I made it
    //a little better...

    var imageArray:Array = (value == null || value.imageArray == null)?
        null : value.imageArray as Array;

    if(imageArray == null) //On first creation of the view I create the data object
    {
        imageArray = new Array(36); //set an array that will cache my images.
        for(var i:int = 0; i<36;i++)
        {
            var img:Image = new Image();
            img.source = 'assets/0'+i.toString()+'.png';
            container.addElement(img);
            imageArray[i] = img;
        }

        super.data = {imageArray:imageArray}
    }
    else//Next time use the save images
    {
        var n:int = imageArray.length;
        for (var j:int = 0; j < n; j++) 
        {
            container.addElement(IVisualElement(imageArray[j]));
        }
    }
}
private var _data:Object = {cache: new ContentCache()};

protected function show_clickHandler(event:MouseEvent):void
{
    this.navigator.pushView(views.MyView, _data);
}
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" title="MyView">

    <fx:Script>
        <![CDATA[
            import spark.components.Image;
            import spark.core.ContentCache;

            override protected function createChildren():void
            {
                super.createChildren();

                //you might want to do a sanity first check to make sure the 
                //data was passed in correctly...

                var cache:ContentCache = ContentCache(this.data.cache);

                for(var i:int = 0; i < 36; i++)
                {
                    var img:Image = new Image();
                    img.contentLoader = cache;
                    img.source = 'assets/0' + i.toString() + '.png';
                    container.addElement(img);
                }
            }
        ]]>
    </fx:Script>

    <s:VGroup id="container" />

</s:View>