2

我正在制作一个小型照片库。我创建了一个 xml 文件并尝试使用 itemrenderer 将它链接到我的列表控件。但是,当我尝试保存文件时,我访问了未定义的属性“数据”错误。我认为我们应该使用“数据”来引用数据对象的当前行。这是我的代码......非常感谢!

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
      xmlns:s="library://ns.adobe.com/flex/spark" 
      xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
 <fx:Declarations>
  <fx:Model id="pictureXML" source="data/pictures.xml"/>
  <s:ArrayList id="pictureArray" source="{pictureXML.picture}"/>
 </fx:Declarations>



 <s:List id="pictureGrid" dataProvider="{pictureArray}" 
   horizontalCenter="0" top="20">
  <s:itemRenderer>
   <fx:Component>
    <s:HGroup>
     <mx:Image source="images/big/{data.source}" /> // where the error happen
     <s:Label text="{data.caption}"/> // where the error happen
    </s:HGroup>   

   </fx:Component>
  </s:itemRenderer>
 </s:List>


</s:Application>

我的 xml

<?xml version="1.0"?>
<album>
   <picture>
   <source>city1.jpg </source>
   <caption>City View 1</caption>
   </picture>
    <picture>
   <source>city2.jpg </source>
   <caption>City View 2</caption>
   </picture>
     <picture>
   <source>city3.jpg </source>
   <caption>City View 3</caption>
   </picture>
    <picture>
   <source>city4.jpg </source>
   <caption>City View 4</caption>
   </picture>
    <picture>
   <source>city5.jpg </source>
   <caption>City View 5</caption>
   </picture>
    <picture>
   <source>city6.jpg </source>
   <caption>City View 6</caption>
   </picture>
    <picture>
   <source>city7.jpg </source>
   <caption>City View 7</caption>
   </picture>
    <picture>
   <source>city8.jpg </source>
   <caption>City View 8</caption>
   </picture>
    <picture>
   <source>city9.jpg </source>
   <caption>City View 9</caption>
   </picture>
    <picture>
   <source>city10.jpg </source>
   <caption>City View 10</caption>
   </picture>
</album>

我很感激任何帮助!!!

4

3 回答 3

3
<s:List id="pictureGrid" dataProvider="{pictureArray}" 
   horizontalCenter="0" top="20">
  <s:itemRenderer>
   <fx:Component>
      <s:ItemRenderer>
    <s:HGroup>
     <mx:Image source="images/big/{data.source}" /> // where the error happen
     <s:Label text="{data.caption}"/> // where the error happen
    </s:HGroup>   
    </s:ItemRenderer>
   </fx:Component>
  </s:itemRenderer>
 </s:List>

尝试这个!它会工作

于 2011-07-31T05:32:26.000 回答
1

我认为您尝试访问的数据属性超出范围,因为它位于内联 itemRenderer 中。尝试将您的 ItemRenderer 抽象到它自己的组件中,而不是将其作为内联组件。

您也可以访问内联 itemRenderer 以访问每个项目数据属性,但这样更简洁......

我已经将您的 IR 分成了自己的组件,一切似乎都很好......

<!-- YOUR LIST -->
<s:List id="pictureGrid" dataProvider="{pictureArray}" 
                horizontalCenter="0" top="20"
                itemRenderer="TestRenderer"/> // reference new IR class here

<!-- NEW ITEMRENDERER CLASS -->
<?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">

        <s:HGroup>
            <mx:Image source="images/big/{data.source}" /> 
            <s:Label text="{data.caption}"/> 
        </s:HGroup>   

</s:ItemRenderer>
于 2011-02-07T21:36:52.790 回答
0

试试这个

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

                irImage.source = data.path; 
                            irText.text = data.caption   

  }


<s:HGroup>
            <mx:Image id="irImage" /> 
            <s:Label id="irText"text="{data.caption}"/> 
</s:HGroup> 

如果这不起作用,请告诉我。

于 2011-05-05T09:45:23.850 回答