简体   繁体   English

自定义项目渲染器在Flex 4中的怪异行为

[英]Weird behavior of custom item renderer in Flex 4

I have a class defined like this: 我有一个这样定义的类:

public class Photo
    {
        public function Photo()
        {
        }
        public var PhotoId:int;
        public var Title :String;
        public var Subtitle :String;
        public var PhotoByteArray:ByteArray ;
        public var ThumbnailByteArray:ByteArray;
        public var ShowOnlyInFrontpageTop:Boolean;
        public var ShowInFrontpageGroup:Boolean;
        public var BackgroundColorGradientFrom:String;
        public var BackgroundColorGradientTo:String;
        public var IsActive :Boolean;
    }

I 'm getting diverse objects of this type (Photo) from a WCF service (works fine!). 我从WCF服务中获得了这种类型的对象(照片)(工作正常!)。 These objects are stored in an ArrayCollection defined like this: 这些对象存储在如下定义的ArrayCollection中:

public static var GroupPhotos:ArrayCollection = new ArrayCollection();

I want to show up these photos using as:List component, like this: 我想使用as:List组件显示这些照片,如下所示:

<s:List height="110" id="GroupPhotos" itemRenderer="MyGroupPhotoRenderer">
                <s:layout >
                    <s:HorizontalLayout requestedColumnCount="3"  />
                </s:layout>
            </s:List>

The item renderer is defined this way: 项目渲染器的定义方式如下:

<?xml version="1.0" encoding="utf-8"?>
<s:ItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009" 
                xmlns:s="library://ns.adobe.com/flex/spark" 
                xmlns:mx="library://ns.adobe.com/flex/mx" 
                autoDrawBackground="true"
                creationComplete="onItemrenderer_creationCompleteHandler(event)">
    <fx:Script>
        <![CDATA[
            import mx.controls.Alert;
            import mx.events.FlexEvent;

            //when the item has been rendered and is ready to be shown the following method will be called
            protected function onItemrenderer_creationCompleteHandler(event:FlexEvent):void
            {               
                img.source =  data.ThumbnailByteArray;
            }

        ]]>
    </fx:Script>

    <s:Group id="PhotoGroup" width="297" height="110" >
        <!--<s:Rect id="imgRectangle" alpha="0" width="95" height="65">
            <s:stroke>
                <s:LinearGradientStroke weight="{GroupBoxBorderWeight}" scaleMode="none"/>
            </s:stroke>
        </s:Rect>-->
        <mx:Image id="img"  
                  width="{PhotoGroup.width}" height="{PhotoGroup.height}" 
                  />
        <s:Label id="title"                  
                 fontSize="20" 
                 text="{data.Title}"/>
    </s:Group>
</s:ItemRenderer>

The s:Label component shows up correctly whereas the mx:Image component shows up always the same image (don't know if this is the first Image or the last in the array). s:Label组件正确显示,而mx:Image组件始终显示同一图像(不知道这是数组中的第一个Image还是最后一个)。 What am i missing? 我想念什么? Thanks in advance 提前致谢

Ahhm!!Turns out that this my error! 啊!原来这是我的错误! Above i stated that the service is running fine: guess what...it didn't! 上面我说服务运行良好:猜猜...没有! Fixing the service made the images show up correctly! 修复该服务可以使图像正确显示!

Creation complete is only called the first time your renderer is created. 仅在首次创建渲染器时才称为完成创建。 Since Flex reuses renderers that means it will only get called the first time. 由于Flex重用渲染器,这意味着它将仅在第一次调用。 Instead of using creationComplete , override the set data method of your renderer. 而不是使用creationComplete ,而是重写渲染器的set data方法。 Unfortunately, s:ItemRenderer doesn't have a set data method, so I'd use a mx:HBox component instead. 不幸的是, s:ItemRenderer没有set data方法,因此我将使用mx:HBox组件。

Here's a quick example to get you started: 这是一个快速入门的示例:

<?xml version="1.0" encoding="utf-8"?>
<mx:HBox xmlns:fx="http://ns.adobe.com/mxml/2009" 
             xmlns:s="library://ns.adobe.com/flex/spark" 
             xmlns:mx="library://ns.adobe.com/flex/mx">
    <fx:Script>
        <![CDATA[

            override public function set data(value:Object):void
            {
                super.data = value;

                theLabel.text = value["Title"].toString();
                theImage.source = data.ThumbnailByteArray;

                validateDisplayList();
            }

        ]]>
    </fx:Script>
    <mx:Label id="theLabel" />
    <mx:Image id="theImage" />
</mx:HBox>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM