在一个子窗口如何使用数据从主窗口? [英] How do I use data from the main window in a sub-window?
问题描述
我刚刚开始工作使用Flex照片查看器型桌面AIR应用程序。从主窗口,我可以启动子窗口,但在这些子窗口,我似乎无法访问我收集在主窗口中的数据。
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
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
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>
我意识到这可能是一个非常简单的问题,但我已经在网上搜索,阅读和观看随机AIR科目教程了几天,但没有找到它。的,看起来像傻瓜的危险,现在是值得的,我想在我的第一个应用程序!
I realize this might be a very easy question but I have searched the web, read and watched tutorials 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.
使用属性和setter函数:
With an attribute and a setter function :
myWindow.mxml:
myWindow.mxml :
<![CDATA[
private var _data : Array;
public function set data(data : Array) : void {
this._data = data;
}
]]>
主要
<![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');
]]>
您也可以做到这一点通过添加一个构造函数的参数到你的窗口,但你必须写在ActionScript的窗口组件。
You could also do it by adding a constructor parameter to your window, but you will have to write your Window component in ActionScript.
。(另外:你可能想使用mywindow的为您的组件,而不是为mywindow的名字,但是这只是conventionnal挑剔)
(Also : you might want to use MyWindow for the name of your component instead of myWindow, but that's just conventionnal nitpicking).
另外,请注意,有一个单变量Application.application,都可以访问应用中的所有类;不过,我不知道这是否适用于所以WindowedApplication,并且无论哪种方式,它是不推荐的方法。
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.
这篇关于在一个子窗口如何使用数据从主窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!