The first tag in an MXML application is either the <s:Application> tag
for the Spark application container, or the <mx:Application> tag
for an MX application container. The application container then
becomes the default container for any content that you add to your
application.
The application object, of type spark.components.Application or mx.core.Application,
is the default scope for any ActionScript code in the file. Therefore,
the ActionScript keyword this refers to the application
object.
You also use the application container to define the initial
size of the application. By default, the application sets its height
to 375 pixels and its width to 500 pixels.
Although you might find it convenient to use an application container
as the only container in your application, usually you explicitly
define at least one more container before you add any controls to
your application. For example, you might use a Panel container as
the first child container of the application.
The application containers have the following default layout
characteristics:
Characteristic
Spark Application container
MX Application container
Default size
375 pixels high and 500 pixels wide in the
Standalone Flash Player, and all available space in a browser.
375 pixels high and 500 pixels wide in the
Standalone Flash Player, and all available space in a browser.
Child layout
BasicLayout, meaning that you have to explicitly
size and position all children.
Vertical column arrangement of children
centered in the Application container.
Default padding
0 pixels.
24 pixels for the paddingTop, paddingBottom, paddingLeft,
and paddingRight properties.
Scroll bars
Added by skinning the container.
Built into the container.
Sizing an application container and its children
You
can set the height and width of an application container by using
explicit pixel values or by using percentage values, where the percentage
values are relative to the size of the browser window. By default,
the application container sets its height to 375 pixels and its
width to 500 pixels in the Standalone Flash Player, and sizes itself
to use all available space in a browser.
The following example sets the size of the Application container
to one-half of the width and height of the browser window:
The advantage of using percentages to specify the size is that
Flex can resize your application as the user resizes the browser
window. Flex maintains the application container size as a percentage
of the browser window as the user resizes it.
If you set the width and height properties
of the child components to percentage values, your components can
also resize as your application resizes, as the following example
shows:
The executing SWF file for the previous example is shown below:
If you want to set a child container to fill the entire application
container, the easiest method is to set the child’s width and height properties
to 100% in MXML. In ActionScript, set the percentWidth and percentHeight properties
to 100.
Because the Spark Application container defines 0 pixels of padding
around its content area, a child sized to 100% fills the entire
area of the container. In the following example, the child VBox
container fills the entire application container:
The executing SWF file for the previous example is shown below:
The MX Application container defines 24 pixels of padding around
all four sides of its content area. Therefore, if you want a child
to occupy the entire area of the Application container, set the
container’s padding properties (paddingTop, paddingBottom, paddingLeft, paddingRight)
to 0.
Using scroll bars with the application container
The MX Application container
has built in support for scroll bars. Therefore, if the children
of the MX Application container are sized or positioned such that
some or all the component is outside the visible area of the MX
Application container, Flex adds scroll bars to the container.
In the following example, the VBox container in the MX Application
container is larger than the available space within the Application
container, which results in scroll bars:
The executing SWF file for the previous example is shown below:
To add scroll bars to the Spark Application container,
define a skin that supports scroll bars. The following example shows
a skin in the file MyAppSkin.mxml. This skin modifies the default
skin for the Spark Application container, spark.skins.default.ApplicationSkin,
to add the Spark Scroller component:
Overriding the default Application container styles
By
default, the application containers have the following properties
and style properties that define the visual aspects of the application
and differ from the default container values:
Property
Spark Application default value
MX Application default value
backgroundColor
The color of the Stage area of Adobe® Flash® Player
or Adobe® AIR™, which
is visible during application loading and initialization. This color
is also visible if the application skin does not define any other
background color or image. The default value is 0xFFFFFF (white).
The color of the Stage area of Adobe® Flash® Player or
Adobe® AIR™, which
is visible during application loading and initialization. This color
is also visible if the application background is transparent. The
default value is 0xFFFFFF (white).
backgroundGradientAlphas
Not implemented; define in the container skin.
[1.0, 1.0], a fully opaque
background.
backgroundGradientColors
Not implemented; define in the container skin.
undefined, which means background gradient
is generated based on the backgroundColor property.
By default, the backgroundColor property defines
a white background.
backgroundImage
Not implemented; define in the container skin.
A gradient controlled by the backgroundGradientAlphas and backgroundGradientColors styles.
The default value is mx.skins.halo.ApplicationBackground.
backgroundSize
Not implemented; define in the container skin.
100%. When you set this property at 100%,
the background image takes up the entire Application container.
horizontalAlign
Not implemented; set in the layout class
associated with the Application container.
Centered.
paddingBottom
0 pixels.
24 pixels.
paddingLeft
0 pixels.
24 pixels.
paddingRight
0 pixels.
24 pixels.
paddingTop
0 pixels.
24 pixels.
Changing the Spark Application container background
The
Spark Application.backgroundColor style defines
the default background color of the Application container during
application loading and initialization. If the Application skin
does not set a different color, the backgroundColor style
also defines the background color of the application when it is
running. The default value is 0xFFFFFF (white).
To set the
background color of the Spark Application container to something other
than a solid color, or to set a background image, you define a skin
for the container. For example, the following skin defines a gradient
fill for the background of the Application container that goes from
blue to gray:
The Application
container backgroundGradientAlphas, backgroundGradientColors,
and backgroundImage styles control the container
background. By default, these properties define an opaque gray gradient
background.
You specify an image for the application background
by using the backgroundImage property. If you set
both the backgroundImage property and the backgroundGradientColors property,
Flex ignores backgroundGradientColors.
You
can specify a gradient background for the application in two ways:
Set the backgroundGradientColors property
to two values, as in the following example:
To set a solid background to the
application by using the backgroundGradientColors property,
specify the same two values to the backgroundGradientColors property,
as the following example shows:
The backgroundColor property
specifies the background color of the Stage area in Flash Player,
which is visible during application loading and initialization,
and a background gradient while the application is running. By default,
the backgroundColor property is set to 0xFFFFFF (white).
If
you use the backgroundGradientColors property to
set the application background, set the backgroundColor property
to compliment the backgroundGradientColors property,
as the following example shows:
In this example,
you use the backgroundGradientColors property to set
a gradient pattern from a dark blue to gray, and the backgroundColor property
to set the Stage area in Flash Player to dark blue, which is visible during
application loading and initialization.
Adding a control bar area to the application container
An application control bar contains a group of controls
outside of the main content area of an application container. The
control bar is always visible, and cannot be scrolled like other
container children.
For the MX Application container,
create a control bar area by adding the ApplicationControlBar container
as a child of the MX Application container. For more information
and examples, see MX ApplicationControlBar layout container.
For the Spark Application container,
use the controlBarContent property to define the
controls that appear in the control bar area, as the following example shows:
The executing SWF file for the previous example is shown below:
The location and appearance of the control bar area of the Spark
Application container is determined by the spark.skins.spark.ApplicationSkin
class, the skin class for the Spark Application container. By default,
the ApplicationSkin class defines the control bar area to appear
at the top of the content area of the Application container with
a grey background. Create a custom skin to change the default appearance
of the control bar.
By default, the controls in the control bar area use horizontal
layout. Use the Application.controlBarLayout property
to specify a different layout, as the following example shows:
The following events are dispatched only by the application
containers:
applicationComplete Dispatched
after the application has been initialized, processed by the LayoutManager,
and attached to the display list. This is the last event dispatched
during an application’s startup sequence. It is later than the application’s creationComplete event,
which gets dispatched before the preloader has been removed and
the application has been attached to the display list.
error Dispatched when an HTTPService call
fails.
Viewing the application source code
You can use the viewSourceURL property
of the application containers to specify a URL to the application’s
source code. If you set this property, Flex adds a View Source menu
item to the application’s context menu, which you open by right-clicking
anywhere in your application. Select the View Source menu item to open
the URL specified by the viewSourceURL property
in a new browser window.
Set the viewSourceURL property by using MXML,
not ActionScript, as the following example shows:
The executing SWF file for the previous example is shown below:
You typically deploy your source code not as an MXML file, but
as a text or HTML file. In this example, the source code is in the
file AppSourceURL.txt. If you use an HTML file to represent your
source code, you can add formatting and coloring to make it easier
to read.
Specifying options of the Application container
You
can specify several options of the application container to control
your application. The following table describes these options:
Option
Type
Description
frameRate
Number
Specifies the frame rate of the application,
in frames per second. The default value is 24.
pageTitle
String
Specifies a String that appears in the title
bar of the browser. This property provides the same functionality
as the HTML <title> tag.
preloader
String
Specifies the path of a SWC component class
or ActionScript component class that defines a custom progress bar.
A
SWC component must be in the same directory as the MXML file or
in the WEB-INF/flex/user_classes directory of your Flex web application.
Specifies the maximum depth of Flash Player
or AIR call stack before Flash Player or AIR stops. This is essentially
the stack overflow limit.
The default value is 1000.
scriptTimeLimit
Number
Specifies the maximum duration, in seconds,
that an ActionScript event listener can execute before Flash Player
or AIR assumes that it has stopped processing and aborts it.
The
default value is 60 seconds, which is also the maximum allowable
value that you can set.
useDirectBlit
Boolean
Specifies whether hardware acceleration
is used to copy graphics to the screen (if such acceleration is
available).
This option only applies to applications running
in the standalone Flash Player. For applications running in a browser,
setting useDirectBlit to true is
equivalent to setting wmode to "direct" in
the HTML wrapper. For AIR applications, use the renderMode application descriptor
tag.
The default value is false.
useGPU
Boolean
Specifies whether GPU (Graphics Processing
Unit) acceleration is used when drawing graphics (if such acceleration
is available).
This option only applies to applications running
in the standalone Flash Player. For applications running in a browser,
setting useGPU to true is equivalent
to setting wmode to "gpu" in the
HTML wrapper. For AIR applications, use the renderMode application
descriptor tag.
The default value is false.
usePreloader
Boolean
Specifies whether to disable the application
preloader (false) or not (true).
The default value is true. To use the default preloader,
your application must be at least 160 pixels wide.
Note: Properties frameRate, pageTitle, preloader, scriptRecursionLimit,
and usePreloader, cannot be set in ActionScript; they
must be set in MXML code.