如何在子窗口中使用主窗口中的数据?
我刚刚开始使用 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 中编写 Window 组件.
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 作为组件的名称,但这只是传统的挑剔).
(Also : you might want to use MyWindow for the name of your component instead of myWindow, but that's just conventionnal nitpicking).
另外,注意有一个单例变量 Application.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.