Window 容器示例
以下示例显示 Window 组件的基本用法。示例包括两个 MXML 文件。第一个文件使用 WindowedApplication 容器,是应用程序的初始窗口。第二个文件使用 Window 容器,以定义应用程序的辅助窗口。在此示例中,主要窗口模拟应用程序的“闪屏”。在设定的时间(4 秒)之后,它关闭闪屏并打开第二个窗口为了使闪屏窗口不带窗口镶边,在 application.xml 文件中将 systemChrome 标签设置为 none。
以下代码定义主应用程序 MXML 文件,此文件包含运行应用程序时自动打开的初始窗口(闪屏):
<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" showFlexChrome="false"
creationComplete="init();">
<mx:Script>
<![CDATA[
private const LOAD_DELAY:int = 4;
private var timeElapsed:int = 0;
private var loadTimer:Timer;
private var docWindow:DocumentWindow;
private function init():void
{
// center the window on the screen
var screenBounds:Rectangle = Screen.mainScreen.bounds;
nativeWindow.x = (screenBounds.width - nativeWindow.width) / 2;
nativeWindow.y = (screenBounds.height - nativeWindow.height) / 2;
// start the timer, which simulates a loading delay
loadTimer = new Timer(1000);
loadTimer.addEventListener(TimerEvent.TIMER, incrementTime);
loadTimer.start();
updateStatus();
}
private function incrementTime(event:TimerEvent):void
{
timeElapsed++;
updateStatus();
// if the loading delay has passed, stop the timer,
// close the splash screen, and open the document window
if ((LOAD_DELAY - timeElapsed) == 0)
{
loadTimer.stop();
loadTimer.removeEventListener(TimerEvent.TIMER, incrementTime);
loadTimer = null;
nativeWindow.close();
// open a new instance of the document window
docWindow = new DocumentWindow();
docWindow.open();
}
}
private function updateStatus():void
{
var timeRemaining:uint = LOAD_DELAY - timeElapsed;
var timeRemainingMsg:String = timeRemaining.toString() + " second";
if (timeRemaining != 1) { timeRemainingMsg += "s"; }
timeRemainingMsg += " remaining.";
loadStatusMessage.text = "initializing... " + timeRemainingMsg;
}
]]>
</mx:Script>
<mx:VBox horizontalCenter="0" verticalCenter="0">
<mx:Text text="My Splash Screen" fontFamily="Courier New" fontSize="36"/>
<mx:Text id="loadStatusMessage" text="initializing..."/>
</mx:VBox>
</mx:Application>
每秒调用一次 incrementTime() 方法,当达到合适时间时则创建 DocumentWindow 实例,并调用其 open() 方法。
DocumentWindow 类定义于单独的 MXML 文档中。其基础 MXML 标签是 <mx:Window> 标签,因此它是Window 类(Window 组件)的子类。
下面是 DocumentWindow MXML 文件的源代码:
<?xml version="1.0" encoding="utf-8"?>
<mx:Window xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute"
title="Document window"
width="550" height="450">
<mx:Text text="This is a document window." horizontalCenter="0" verticalCenter="0"/>
</mx:Window>