使用弹出子窗口以及子窗口数据回传(转)
简单做了个带登陆子窗口的app做试验。
子窗口的使用参照help文档中的相关主题使用PopUpManager.createPopUp(...)方法实现,不过参考网上一些朋友的blog中所说的“使用parent访问父对象的属性来回传数据”的方法似乎有点出入,怀疑是版本不同的原因,经过尝试,最终使用以下代码获取父窗口:
var obj:Default = Default(this.parentApplication); //其中Default是父窗口的文件名。
最终的代码如下,首先是登陆子窗口LoginWindow.mxml:
<?xml version="1.0" encoding="utf-8"?>
<mx:TitleWindow xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" width="100%"
height="100%" creationComplete="doInit();" title="Login Window">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.managers.FocusManager;
import mx.core.Application;
import mx.managers.PopUpManager;
private function doInit():void
{
PopUpManager.centerPopUp(this);
}
private function processLogin():void
{
this.lb1.text = "";
if(this.username.text=="")
{
this.lb1.text = "用户名不能为空。";
return;
}
if(this.password.text==this.username.text+"1")
{
try
{
var obj:Default = Default(this.parentApplication);
obj.ShowUser(this.username.text);
}
catch(e:Error)
{
Alert.show(e.message, 'Error');
}
PopUpManager.removePopUp(this);
}
else
{
this.password.text = "";
this.password.focusEnabled = true;
this.lb1.text = "用户名或密码错误。";
}
}
]]>
</mx:Script>
<mx:Form>
<mx:FormItem label="User Name">
<mx:TextInput id="username" width="100%"/>
</mx:FormItem>
<mx:FormItem label="Password">
<mx:TextInput id="password" displayAsPassword="true" width="100%"/>
</mx:FormItem>
</mx:Form>
<mx:HBox>
<mx:Button click="processLogin();" label="OK"/>
<mx:Button click="this.username.text=''; this.password.text='';" label="Reset"/>
<mx:Label id="lb1" text="" color="red" />
</mx:HBox>
</mx:TitleWindow>
<mx:TitleWindow xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" width="100%"
height="100%" creationComplete="doInit();" title="Login Window">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.managers.FocusManager;
import mx.core.Application;
import mx.managers.PopUpManager;
private function doInit():void
{
PopUpManager.centerPopUp(this);
}
private function processLogin():void
{
this.lb1.text = "";
if(this.username.text=="")
{
this.lb1.text = "用户名不能为空。";
return;
}
if(this.password.text==this.username.text+"1")
{
try
{
var obj:Default = Default(this.parentApplication);
obj.ShowUser(this.username.text);
}
catch(e:Error)
{
Alert.show(e.message, 'Error');
}
PopUpManager.removePopUp(this);
}
else
{
this.password.text = "";
this.password.focusEnabled = true;
this.lb1.text = "用户名或密码错误。";
}
}
]]>
</mx:Script>
<mx:Form>
<mx:FormItem label="User Name">
<mx:TextInput id="username" width="100%"/>
</mx:FormItem>
<mx:FormItem label="Password">
<mx:TextInput id="password" displayAsPassword="true" width="100%"/>
</mx:FormItem>
</mx:Form>
<mx:HBox>
<mx:Button click="processLogin();" label="OK"/>
<mx:Button click="this.username.text=''; this.password.text='';" label="Reset"/>
<mx:Label id="lb1" text="" color="red" />
</mx:HBox>
</mx:TitleWindow>
在主窗口Default.xml中添加以下内容:
//……
private function OnInit():void
{
showLogin();
}
private function showLogin():void
{
var win:TitleWindow = TitleWindow(
PopUpManager.createPopUp(this, Components.LoginWindow, true));
win.setStyle("borderAlpha", 0.9);
}
public function ShowUser(un:String):void
{
this.lblUser.text = "早上好,"+un;
}
//……
<mx:Label id="lblUser" text="早上好" paddingLeft="20"/>
//……
private function OnInit():void
{
showLogin();
}
private function showLogin():void
{
var win:TitleWindow = TitleWindow(
PopUpManager.createPopUp(this, Components.LoginWindow, true));
win.setStyle("borderAlpha", 0.9);
}
public function ShowUser(un:String):void
{
this.lblUser.text = "早上好,"+un;
}
//……
<mx:Label id="lblUser" text="早上好" paddingLeft="20"/>
//……