簡體   English   中英

自定義項目渲染器在Flex 4中的怪異行為

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

我有一個這樣定義的類:

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;
    }

我從WCF服務中獲得了這種類型的對象(照片)(工作正常!)。 這些對象存儲在如下定義的ArrayCollection中:

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

我想使用as:List組件顯示這些照片,如下所示:

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

項目渲染器的定義方式如下:

<?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>

s:Label組件正確顯示,而mx:Image組件始終顯示同一圖像(不知道這是數組中的第一個Image還是最后一個)。 我想念什么? 提前致謝

啊!原來這是我的錯誤! 上面我說服務運行良好:猜猜...沒有! 修復該服務可以使圖像正確顯示!

僅在首次創建渲染器時才稱為完成創建。 由於Flex重用渲染器,這意味着它將僅在第一次調用。 而不是使用creationComplete ,而是重寫渲染器的set data方法。 不幸的是, s:ItemRenderer沒有set data方法,因此我將使用mx:HBox組件。

這是一個快速入門的示例:

<?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