0

我有一个页面(下)有一个数据网格,其中列出了从 XML 文件(下)返回的“项目”,当它加载它时,它在视图堆栈中为它找到(工作)的每个项目创建了一个页面,它还呈现一个页面(下)在每个面板内,但我在传递数据时遇到问题。每个页面都呈现和加载,但使用 XML 数据的第一个结果,而不是每个页面都有自己的数据,它们都与第一页相同。

主页.mxml

<?xml version="1.0" encoding="utf-8"?>
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml"
 width="100%" height="100%"
 creationComplete="init()"
 xmlns:software="com.xd.components.pages.software.*"
 xmlns:pages="com.xd.components.pages.*"
xmlns:renderers="com.xd.components.renderers.*">
<mx:Script>
<![CDATA[
import mx.core.Application;
import com.xd.components.renderers.PageListRenderer;
import mx.containers.Panel;
import mx.core.Container;
import com.xd.components.renderers.SoftwareListRenderer;
import mx.utils.ObjectProxy;
import mx.controls.Alert;
import mx.rpc.events.ResultEvent;
import mx.containers.VBox;
[Bindable] public var httpData:Object;
[Bindable] public var selectedItem:String;

        private function init():void {
            //get data
            httpService.send();    
        }

        //private const MAX_CHILDREN:uint = 5;

        private function viewstack_addChild(name:String):void {
            var p:Panel = new Panel();
            p.id = name;
            p.name = name;
            p.title = name;
            p.percentWidth = 100;
            p.percentHeight = 100;
            var randColor:uint = Math.random() * 0xFFFFFF;
            p.setStyle("backgroundColor", randColor);
            var pR:PageListRenderer = new PageListRenderer();
            //var data:Object;
            //Do something to get the data to be displayed;
            //data = httpService.lastResult.item;
            pR.data = httpData;
            p.addChild(pR);
            myViewStack.addChild(p);
        }
        private function httpResult_handler(evt:ResultEvent):void {
            if (evt.result.software.item) {

             httpData = XML(evt.result).descendants("item");
             var item:Object = httpData;
             for each(item in httpData) {
              viewstack_addChild(item.name);
             }
            }
        }
     public function switchView(viewId:String):void
     {
         var container:Container = Container(myViewStack.getChildByName(viewId));
         selectedItem = viewId;
         if (container != null)
         {
             myViewStack.selectedChild = container;
         }
     }
    ]]>
</mx:Script>

<mx:HTTPService id="httpService" url="data/Software.xml" resultFormat="e4x" result="httpResult_handler(event)" fault="Alert.show('XML Data Error')" />

<mx:VBox width="100%" height="100%" horizontalAlign="center" verticalAlign="middle">
<mx:HBox width="100%" height="100%">
<mx:VBox width="30%" height="100%">
<mx:Panel borderThicknessLeft="0" borderThicknessRight="0" borderThicknessBottom="0" width="100%" height="100%" title="Software List:">
    <mx:DataGrid id="myDG" 
                dataProvider="{httpData}"
                headerHeight="0"
                editable="false"
                width="100%" height="100%" 
                rowHeight="50"
                itemClick="switchView(myDG.selectedItem.name);">

        <mx:columns>
            <mx:DataGridColumn itemRenderer="com.xd.components.renderers.SoftwareListRenderer" />
        </mx:columns>

    </mx:DataGrid>
</mx:Panel>
</mx:VBox>
<mx:VBox width="70%" height="100%">
 <mx:ViewStack id="myViewStack" width="100%" height="100%" hideEffect="WipUp" showEffect="WipeDown" creationPolicy="all">
   <pages:NewsPage id="NewsPage" height="100%" width="100%" horizontalScrollPolicy="off" verticalScrollPolicy="off" />
   <software:ShowTracker id="ShowTracker" height="100%" width="100%" horizontalScrollPolicy="off" verticalScrollPolicy="off" />
 </mx:ViewStack>
</mx:VBox>
</mx:HBox>
</mx:VBox>
</mx:Canvas>

页面列表渲染器.mxml

<?xml version="1.0" encoding="utf-8"?>
<mx:HBox 
height="100%" 
width="100%" 
xmlns:mx="http://www.adobe.com/2006/mxml">

<mx:Script>
 <![CDATA[
  import mx.core.Application;
 ]]>
</mx:Script>
<mx:VBox width="100%" height="100%">
<mx:VBox height="70%" width="100%" horizontalAlign="center" verticalAlign="middle">
  <mx:Image source="{data.image}"  width="90%" height="90%"/>
</mx:VBox>
 <mx:VBox height="30%" width="100%" horizontalAlign="left" verticalAlign="top">
  <mx:Label width="100%" text="Name / Version:" color="#FFFFFF"/>
     <mx:Label width="100%" text="{data.name}" color="#FFAE00"/>
     <mx:Label width="100%" text="{data.version}" color="#FFAE00"/>
     <mx:Label width="100%" text="{data.longdesc}" color="#FFFFFF"/>
<mx:Button click="installApp()" label="Install Application" horizontalCenter="0" verticalCenter="0" width="160" height="52" id="install" visible="false"/>
  <mx:Button click="launchApp()" label="Launch Application" horizontalCenter="0" verticalCenter="0" width="160" height="52" id="launch" visible="false"/>
 </mx:VBox> 
</mx:VBox> 
</mx:HBox>

软件.xml

<software>

<item>
<name>ShowTracker</name>
<version>1.0</version>
<shortdesc>TV Show Tracker.</shortdesc>
<image></image>
<longdesc>ShowTracker allows you to manage all the TV Shows you watch. I helps you keep track of what episode you are upto for each series you are currently watching</longdesc>
<appid>com.showtracker</appid>
<pubid>EB6DA1B683461BF1C71FEC13E276982941DF167B.1</pubid>
<appurl>http://www.nitcom.com.au/showtracker/downloads/ShowTracker-v1.air</appurl>
<runversion>1.5</runversion>
</item>
<item>
<name>Test2</name>
<version>1</version>
<shortdesc>test desc #2</shortdesc>
<image>test1.jpg</image>
<longdesc>test1.jpg</longdesc>
<appid>com.showtracker</appid>
<pubid>EB6DA1B683461BF1C71FEC13E276982941DF167B.1</pubid>
<appurl>http://www.nitcom.com.au/showtracker/downloads/ShowTracker-v1.air</appurl>
<runversion>1.5</runversion>
</item>
<item>
<name>Test #3</name>
<version>1</version>
<shortdesc>test desc #3</shortdesc>
<image>test1.jpg</image>
<longdesc>test1.jpg</longdesc>
<appid>com.showtracker</appid>
<pubid>EB6DA1B683461BF1C71FEC13E276982941DF167B.1</pubid>
<appurl>http://www.nitcom.com.au/showtracker/downloads/ShowTracker-v1.air</appurl>
<runversion>1.5</runversion>
</item>
</software>
4

1 回答 1

0

不知道这是否导致问题,但它看起来像这样:

private function httpResult_handler(evt:ResultEvent):void {
     if (evt.result.software.item) {

       httpData = XML(evt.result).descendants("item");
       var item:Object = httpData;
       for each(item in httpData) {
          viewstack_addChild(item.name);
       }
    }
 }

需要是这样的:

private function httpResult_handler(evt:ResultEvent) :void {
  if(evt.result.software.item){
    var httpData:XMLList = XML(evt.result).descendants("item");
    for each(var item:Object in httpData){
      viewstack_addChild(item.name);
    }
  }
}

实际上,您应该将整个项目传递给视图堆栈,并让它获取所需的内容,而不是向其发送单个属性。

于 2009-11-10T04:35:31.317 回答