Creating and Extending Flex 2 Components
Adobe
®
Flex
™
2
© 2006 Adobe Systems Incorporated. All rights reserved.
Creating and Extending Flex™ 2 Components
If this guide is distributed with software that includes an end-user agreement, this guide, as well as the software described in it, is
furnished under license and may be used or copied only in accordance with the terms of such license. Except as permitted by any
such license, no part of this guide may be reproduced, stored in a retrieval system, or transmitted, in any form or by any means,
electronic, mechanical, recording, or otherwise, without the prior written permission of Adobe Systems Incorporated. Please note
that the content in this guide is protected under copyright law even if it is not distributed with software that includes an end-user
license agreement.
The content of this guide is furnished for informational use only, is subject to change without notice, and should not be
construed as a commitment by Adobe Systems Incorporated. Adobe Systems Incorporated assumes no responsibility or liability
for any errors or inaccuracies that may appear in the informational content contained in this guide.
Please remember that existing artwork or images that you may want to include in your project may be protected under copyright
law. The unauthorized incorporation of such material into your new work could be a violation of the rights of the copyright
owner. Please be sure to obtain any permission required from the copyright owner.
Any references to company names in sample templates are for demonstration purposes only and are not intended to refer to any
actual organization.
Adobe, the Adobe logo, Flex, Flex Builder and Flash Player are either registered trademarks or trademarks of Adobe Systems
Incorporated in the United States and/or other countries. Windows is either a registered trademark or trademark of Microsoft
Corporation in the United States and/or other countries. Macintosh is a trademark of Apple Computer, Inc., registered in the
United States and other countries. All other trademarks are the property of their respective owners.
This product includes software developed by the Apache Software Foundation (http://www.apache.org/). Macromedia Flash 8
video is powered by On2 TrueMotion video technology. © 1992-2005 On2 Technologies, Inc. All Rights Reserved. http://
www.on2.com. This product includes software developed by the OpenSymphony Group (http://www.opensymphony.com/).
Portions licensed from Nellymoser (www.nellymoser.com). Portions utilize Microsoft Windows Media Technologies. Copyright
(c) 1999-2002 Microsoft Corporation. All Rights Reserved. Includes DVD creation technology used under license from Sonic
Solutions. Copyright 1996-2005 Sonic Solutions. All Rights Reserved. This Product includes code licensed from RSA Data
Security. Portions copyright Right Hemisphere, Inc. This product includes software developed by the OpenSymphony Group
(http://www.opensymphony.com/).
Sorenson™ Spark™ video compression and decompression technology licensed from Sorenson Media, Inc.
Adobe Systems Incorporated, 345 Park Avenue, San Jose, California 95110, USA
Notice to U.S. government end users. The software and documentation are “Commercial Items,” as that term is defined at 48
C.F.R. §2.101, consisting of “Commercial Computer Software” and “Commercial Computer Software Documentation,” as such
terms are used in 48 C.F.R. §12.212 or 48 C.F.R. §227.7202, as applicable. Consistent with 48 C.F.R. §12.212 or 48 C.F.R.
§§227.7202-1 through 227.7202-4, as applicable, the Commercial Computer Software and Commercial Computer Software
Documentation are being licensed to U.S. Government end users (a) only as Commercial items and (b) with only those rights as
are granted to all other end users pursuant to the terms and conditions herein. Unpublished-rights reserved under the copyright
laws of the United States. Adobe Systems Incorporated, 345 Park Avenue, San Jose, CA 95110-2704, USA. For U.S.
Government End Users, Adobe agrees to comply with all applicable equal opportunity laws including, if appropriate, the
provisions of Executive Order 11246, as amended, Section 402 of the Vietnam Era Veterans Readjustment Assistance Act of
1974 (38 USC 4212), and Section 503 of the Rehabilitation Act of 1973, as amended, and the regulations at 41 CFR Parts 60-1
through 60-60, 60-250 ,and 60-741. The affirmative action clause and regulations contained in the preceding sentence shall be
incorporated by reference.
Part Number: 90069416 (12/06)
3
Contents
Chapter 1: About Flex Documentation . . . . . . . . . . . . . . . . . . . . . . . 7
PART 1: CREATING CUSTOM FLEX COMPONENTS
Chapter 2: Creating Flex Components . . . . . . . . . . . . . . . . . . . . . . 13
About creating components. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Creating custom components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Where to go from here . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Chapter 3: Using ActionScript to Create Components . . . . . . . 25
Using ActionScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Chapter 4: Creating Custom Events . . . . . . . . . . . . . . . . . . . . . . . 35
About events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Dispatching custom events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
Chapter 5: Using Metadata Tags in Custom Components. . . . . 45
About metadata tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Metadata tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
Chapter 6: Compiling Components. . . . . . . . . . . . . . . . . . . . . . . . .63
About compiling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
Compiling components with Flex 2 SDK . . . . . . . . . . . . . . . . . . . . . . . . 66
Compiling components with Flex Data Services. . . . . . . . . . . . . . . . . . .72
PART 2: CREATING MXML COMPONENTS
Chapter 7: Creating Simple MXML Components . . . . . . . . . . . . . 77
About MXML components. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .77
Scoping in custom components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
Applying styles to your custom component . . . . . . . . . . . . . . . . . . . . . . 84
4Contents
Chapter 8: Creating Advanced MXML Components . . . . . . . . . . 91
About reusable MXML components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
Adding custom properties and methods to a component . . . . . . . . . . 92
Working with events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
About interfaces. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .114
PART 3: CREATING ACTIONSCRIPT COMPONENTS
Chapter 9: Creating Simple Visual Components in ActionScript . .
121
About ActionScript components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .121
Adding properties and methods to a component . . . . . . . . . . . . . . . . . 125
Defining events in ActionScript components . . . . . . . . . . . . . . . . . . . . 140
Applying styles to custom components . . . . . . . . . . . . . . . . . . . . . . . . . 144
Chapter 10: Creating Advanced Visual Components in
ActionScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
About creating advanced components. . . . . . . . . . . . . . . . . . . . . . . . . . 147
Implementing the component . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
Making components accessible. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
Adding version numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
Best practices when designing a component . . . . . . . . . . . . . . . . . . . . 172
Example: Creating a composite component . . . . . . . . . . . . . . . . . . . . . 172
Troubleshooting. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
Chapter 11: Creating Custom Style Properties . . . . . . . . . . . . . . 183
About styles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
Example: Creating style properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
Chapter 12: Creating Template Components . . . . . . . . . . . . . . . 195
About template components. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195
Implementing a template component . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
PART 4: CREATING NONVISUAL FLEX COMPONENTS
Chapter 13: Creating Custom Formatters . . . . . . . . . . . . . . . . . 207
Creating a custom formatter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208
Using the SwitchSymbolFormatter class. . . . . . . . . . . . . . . . . . . . . . . . .211
Extending a Formatter class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214
Contents 5
Chapter 14: Creating Custom Validators . . . . . . . . . . . . . . . . . . . 217
Validating data by using custom validators. . . . . . . . . . . . . . . . . . . . . . . 217
Example: Creating a simple validator . . . . . . . . . . . . . . . . . . . . . . . . . . . .219
Example: Validating multiple fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
Chapter 15: Creating Effects . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
About creating a custom effect . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228
About tween effects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
Writing an effect for a transition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240
Defining a custom effect trigger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
评论0