开发者

How do I use data from the main window in a sub-window?

I've just started working on a photo viewer type desktop AIR app with Flex. From the main window I can launch sub-windows, but in these sub-windows I can't seem to access the data I collected in the main window.

How can I access this data?

Or, how can I send this data to the sub-window on creation? It doesn't need to be dynamically linked.

myMain.mxml

    <?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
                       xmlns:s="library://ns.adobe.com/flex/spark"
                       xmlns:mx="library://ns.adobe.com/flex/mx"
                       width="260" height="200"
                       title="myMain">
    <fx:Declarations>
    </fx:Declarations>
    <fx:Script>
        <![CDATA[
            public function openWin():void {
                new myWindow().open();
            }

            public var myData:Array = new Array('The Eiffel Tower','Paris','John Doe');
        ]]>
    </fx:Script>
    <s:Button x="10" y="10" width="240" label="open a sub-window" click="openWin();"/>
</s:WindowedApplication>

myWindow.mxml

    <?xml version="1.0" encoding="utf-8"?>
<mx:Window name="myWindow"
           title="myWindow"
           xmlns:mx="http://www.adobe.com/2006/mxml"
           layout="absolute"
           width="640" height="360">
    <mx:Script>
        <![CDATA[

        ]]>
    </mx:Script>
    <mx:Label id="comment" x="10" y="10" text=""/>
    <mx:Label id="location" x="10" y="30" text=""/>
    <mx:Label id="author" x="10" y="50" text=""/>
</mx:Window>

I realize this might be a very easy question but I have searched the web, read and watched tutorials 开发者_JS百科on random AIR subjects for a few days and couldn't find it. The risk of looking like a fool is worth it now, I want to get on with my first app!


You could add an attribute to your window class, and pass the data from the application.

With an attribute and a setter function :

myWindow.mxml :

    <![CDATA[
        private var _data : Array;

        public function set data(data : Array) : void {
             this._data = data;
        }

    ]]>

main

    <![CDATA[
        public function openWin():void {
            var w : myWindow = new myWindow();
            w.data = myData;
            w.open();
        }

        public var myData:Array = new Array('The Eiffel Tower',
                                            'Paris','John Doe');
    ]]>

You could also do it by adding a constructor parameter to your window, but you will have to write your Window component in ActionScript.

(Also : you might want to use MyWindow for the name of your component instead of myWindow, but that's just conventionnal nitpicking).

Also, note that there is a singleton variable Application.application that is accessible to all classes in an Application ; however I don't know if this applies to a WindowedApplication, and either way it is not the recommended approach.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜