2 May 2011
Beginning
In this exercise you will use Flash Builder to create an MXML class and reuse the class to display employee information (see Figure 1).
In this exercise you will learn how to:
In this section you will add Flex controls to the application.
text property with a value of Employee Portal.Style tag to your code.<fx:Style source="Styles.css"/>
Label control, add a height property with a value of 40, a width property with a value of 300, and a styleName property with a value of titleHeader.<s:Label x="10" y="10"
height="40" width="300"
text="Employee Portal"
styleName="titleHeader"/>
You should see the style applied to the Label control (see Figure 3).
The application should look as shown in Figure 7.
Image control and change it from Image to BitmapImage.<s:BitmapImage x="10" y="58"
source="images/aparker.jpg"/>
Note: It is a best practice to only use the Image control if you need to skin an image (for instance, with a background or border).To just display an image, use BitmapImage.
Note that your application has not changed.
In this section you create a custom component to use in place of the controls added to the application in the last section.
The New MXML Component dialog box should look as shown in Figure 9.
BitmapImage and Label controls that are used for the employee's name and paste them into the EmployeeDisplay.mxml below the Declarations block.Note: You are only cutting the BitmapImage and Label controls that are specific to the employee. Do not cut the Employee Portal Label control.
<?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">
<fx:Declarations>
</fx:Declarations>
<s:BitmapImage x="10" y="58" source="images/aparker.jpg"/>
<s:Label x="10" y="140" text="Athena Parker"/>
</s:Group>
BitmapImage control, delete the x and y properties:<s:BitmapImage source="images/aparker.jpg"/>
Label control's x property to 0 and y property to 80:<s:Label x="0" y="80"
text="Athena Parker"/>
UI components comment, add an instance of the custom component by typing the characters <Empl and then pressing Enter when the content assist tool displays the custom component (see Figure 10).
Application tag, ensure the xml namespace components has been generated. If not, add the namespace assigned to the contents of the components directory.<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"
minWidth="955"
minHeight="600"
xmlns:components="components.*">
EmployeeDisplay component, add the x property with a value of 10 and the y property with a value of 60.<components:EmployeeDisplay x="10" y="60"/>
x property value to 105 and make sure the y property value is 60.You should see that the application contains two EmployeeDisplay components, as shown in Figure 12.
EmployeeDisplay component and notice that it is presented as a tag block. EmployeeDisplay tag and modify the opening tag so that it becomes a single instance of the component.<components:EmployeeDisplay x="10" y="60"/>
<components:EmployeeDisplay x="105" y="60"/>
In this section, you will create two class properties and use them to display data.
Group container tag, create a Script block....
xmlns:mx="library://ns.adobe.com/flex/mx">
<fx:Script>
<![CDATA[
]]>
</fx:Script>
Script block, type imageFile and use the quick assist tool, by pressing CTRL+1, to create a variable. Make the variable public and data typed to the String class. <![CDATA[
[Bindable]
public var imageFile:String;
]]>
Bindable public variable named fullName data typed to the String class by using the content assist and quick assist tools.[Bindable]
public var imageFile:String;
[Bindable]
public var fullName:String;
source property of the BitmapImage control, replace the hard-coded employee image file name with the value of the imageFile variable.<s:BitmapImage source="images/{imageFile}"/>
text property to the value of the fullName variable.<s:Label x="0" y="80" text="{fullName}"/>
imageFile property with a value of aparker.jpg.<components:EmployeeDisplay x="10" y="60" imageFile="aparker.jpg"/>
fullName variable set to the value of Athena Parker.<components:EmployeeDisplay x="10" y="60" imageFile="aparker.jpg"
fullName="Athena Parker" />
imageFile property set to stucker.jpg and the fullName property set to Saul Tucker.<components:EmployeeDisplay x="105" y="60"
imageFile="stucker.jpg"
fullName="Saul Tucker"/>
The custom component now displays two different employees (see Figure 13).
In this exercise you learned to use Flash Builder to create an MXML class and reuse the class to display data. In the next exercise, you will use inline ActionScript to display the date selected from a DateChooser control. You will then use a function to achieve the same result as the inline ActionScript.
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.