Application Object in Flex4


 

Application Object in Flex4

In Flex development, your application have or . In most cases your application have one application object if you do not use SWF loader in your application.

In Flex development, your application have or . In most cases your application have one application object if you do not use SWF loader in your application.

Application object in flex4:

In Flex development, your application have <s:Application/> or <mx:Application/>. In most cases your application have one application object if you do not use SWF loader in your application. If you use SWF loader then your application have more then one application objects. Flex will not compile your application that don't have <s:Application/> or <mx:Application>. Application object are created when your SWF file is loaded. You can use mx.core.FlexGlobals.topLevelApplication to refer the application object.  Every mxml file is called document class. If you want to access methods and properties of top level application object anywhere then you can use topLevelApplication property of the FlexGlobals class. You can see in the example that  how to use topLevelApplication property.

Example:

At first we will create a maincomponent.mxml in the mycomponent package that is shown in below example code.

maincomponent.mxml

<?xml version="1.0" encoding="utf-8"?>

<s:Group 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="400" height="300">

<fx:Script>

<![CDATA[

import mx.core.Application;

import mx.core.FlexGlobals;

]]>

</fx:Script>

<mx:VBox>

<s:Button label="Click" click="myText.text=FlexGlobals.topLevelApplication.myName();"/>

<mx:Text id="myText"/>

</mx:VBox>

</s:Group>

After that call the maincomponent.mxml into mainapplication.mxml file.

mainapplication.mxml

<?xml version="1.0" encoding="utf-8"?>

<s:Application 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="50%" height="300" xmlns:myCom="myComponent.*;" xmlns:myComponent="myComponent.*">

<fx:Script>

<![CDATA[

public function myName():String{

return "My name is Singh";

}

]]>

0

</fx:Script>

<myComponent:maincomponemt/>

</s:Application>

1

Output:

Running Application:

To view this page ensure that Adobe Flash Player version 10.0.0 or greater is installed.

Download maincomponent.mxml file code

2

Download mainapplication.mxml file code

Ads