如何将ArrayCollection发送到新的/子窗口?

时间:2011-07-01 02:41:54

标签: flex air

昨天,phtrivier向我展示了how to send an array to a new/sub-window

现在,我已将此静态数据源替换为加载到ArrayCollection中的XML文件。不幸的是,当您尝试将其中一部分发送到新的/子窗口时,我发现ArrayCollection的行为与数组不同。

如何使用ArrayCollection执行此操作?

或者我应该采用发送数组的简单方法,而是寻找一种方法将XML加载到数组而不是ArrayCollection?我认为我不会要求 AC提供的额外功能。

MyMain.mxml

<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication ...stuff... creationComplete="settingService.send()">
    <fx:Declarations>
        <s:HTTPService id="settingService" url="data.xml" result="settingService_resultHandler(event)"/>
    </fx:Declarations>
    <fx:Script>
        <![CDATA[
            // import dependencies
            import mx.collections.ArrayCollection;
            import mx.rpc.events.ResultEvent;

            // variables
            [Bindable] private var xmlData:ArrayCollection;

            // collect static data
            private var staticData1:Array = new Array('The Eiffel Tower','Paris','John Doe');
            private var staticData2:Array = new Array('The Strip','Las Vegas','Jane Doe');
            private var staticData:Array = new Array(staticData1, staticData2);

            // collect xml data
            protected function settingService_resultHandler(event:ResultEvent):void
            {
                xmlData = event.result.settings.photo;
            }

            // open window & send data in Array, WORKING
            public function openWin1(inData:Array):void
            {
                var w:MyWindow1 = new MyWindow1();
                w.inData = inData;
                w.open();
            }

            // open window & send data in ArrayCollection, NOT WORKING
            public function openWin2(inData:ArrayCollection):void
            {
                var w:MyWindow2 = new MyWindow2();
                w.inData = inData;
                w.open();
            }
        ]]>
    </fx:Script>
    <!--opening windows, adding an array, WORKING-->
    <s:Button x="10" y="10" width="240" label="open a sub-window 1" click="openWin1(staticData[0]);"/>
    <s:Button x="10" y="30" width="240" label="open a sub-window 2" click="openWin1(staticData[1]);"/>
    <!--opening windows, adding an arraycollection, NOT WORKING-->
    <s:Button x="10" y="60" width="240" label="open a sub-window 1" click="openWin2(xmlData.getItemAt(5));"/>
    <s:Button x="10" y="80" width="240" label="open a sub-window 2" click="openWin2(xmlData[5].source);"/>
    <s:Button x="10" y="100" width="240" label="open a sub-window 3" click="openWin2(xmlData.getItemAt(5).source);"/>
</s:WindowedApplication>

MyWindow1.mxml(应该没问题,毕竟还在工作)

<?xml version="1.0" encoding="utf-8"?>
<mx:Window ...stuff...>
    <mx:Script>
        <![CDATA[
            // variables
            [Bindable] private var windowData:Array;

            // receive data
            public function set inData(outData:Array):void {
                this.windowData = outData;
            }
        ]]>
    </mx:Script>
    <mx:TextInput id="comment" x="10" y="10" text="{windowData[0]}"/>
    <mx:TextInput id="location" x="10" y="30" text="{windowData[1]}"/>
    <mx:TextInput id="author" x="10" y="50" text="{windowData[2]}"/>
</mx:Window>

MyWindow2.mxml

<?xml version="1.0" encoding="utf-8"?>
<mx:Window ...stuff...>
    <mx:Script>
        <![CDATA[
            // import dependencies
            import mx.collections.ArrayCollection;

            // variables
            [Bindable] private var windowData:ArrayCollection;

            // receive data
            public function set inData(outData:ArrayCollection):void {
                this.windowData = outData;
            }
        ]]>
    </mx:Script>
    <mx:TextInput id="comment" x="10" y="10" text="{windowData.comment}"/>
    <mx:TextInput id="location" x="10" y="30" text="{windowData.location}"/>
    <mx:TextInput id="author" x="10" y="50" text="{windowData.author}"/>
</mx:Window>

1 个答案:

答案 0 :(得分:0)

找到我自己的解决方案:加载的XML以ArrayCollection形式出现,但无论如何我需要通过转发器运行它。当通过转发器运行时,内部的数组成为对象。我和ObjectsCollections的对象一样多,但是试用和试用。错误我找到了以下工作代码。耶!

// MyMain.mxml // inside the repeater
click="openWin(event.currentTarget.getRepeaterItem())"

// MyMain.mxml // opening the window and sending the data
private function openWin(transferData:Object):void
{
    var w:MyWindow = new MyWindow();
    w.transferData = transferData;
    w.open();
}

// MyWindow.mxml // variables
[Bindable] private var windowData:Object;

// MyWindow.mxml // receive the data
public function set transferData(transferData:Object):void
{
    this.windowData = transferData;
}

// MyWindow.mxml // use the data
<mx:Label text="{windowData.author}"/>

虽然这不是发送一个ArrayCollection,但它解决了我的问题,并且可能使发送实际的ArrayCollection非常容易。