Document directory
- <Declarations/>
- <Vector/>
- <Library/> and <definition/>
- <Private/>
- <Reparent/>
This chapter is Chapter 4th of the new feature series of the Flex 4 SDK. This chapter introduces the new labels in mxml 2009, including:
- Declarations
- Vector
- Library
- Definition
- Private
- Reparent
<Declarations/>
The declarations tag is the most commonly used new tag in flex 4.
In Flex 3, direct child elements of an mxml component can be in the following situations:
1. Attribute tag, for example:
<mx:Label xmlns:mx="http://www.adobe.com/2006/mxml"> <mx:text> <mx:String>test</mx:String> </mx:text></mx:Label>
2. As the default attribute value, for example:
The default attribute of list is dataprovider.
<mx:List xmlns:mx="http://www.adobe.com/2006/mxml"> <mx:ArrayCollection> .... </mx:ArrayCollection></mx:List>
3. Children as a container:
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml"> <mx:Button /> <mx:Label /></mx:Canvas>
4. Declare and create non-visual objects:
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml"> <mx:HTTPService id="sevice" /></mx:Canvas>
In Flex 4, direct child elements of mxml components are more standardized, that is, all direct child elements, Component Attribute tags, or the default attribute values of components exist.The default attribute of the container is the set of its children (for example, the default attribute of group is mxmlcontent and the default attribute of volume roup is dataprovider ).
In the last case, you must use the <declarations/> label to create non-visualized objects, such as httpservice, effect, and basic data types, in mxml In Flex 4. For example, declare an httpservice object:
<s:Group xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library:ns.adobe.com/flex/spark" > <fx:Declarations> <s:HTTPService id="service" /> </fx:Declarations></s:Group>
The above code is equivalent to the following:
public MyGroup extends Group { private var service:HTTPService = new HTTPService();}
It is worth noting that,The declarations tag can also be used to declare and create any visualization components.However, components created using the label are not added to the displaylist or initialized. You can use it as needed.
<Vector/>
Vector is a newly added data type (structure) in Flash Player 10. In most cases, using vector is more efficient than using array. In Flex 4, you can use mxml to create a vector object, for example:
<fx:Declarations> <fx:Vector id="myVector" type="String" fixed="false" /></fx:Declarations>
The above code is equivalent to the following:
public var myVector:Vector.<String> = new Vector.<String>(0,false);
<Library/> and <definition/>
The library tag allows you to declare a "class library" for the current mxml component. Accordingly, the definition label can define a "class" in the class library ". The basic syntax rules are as follows:
- If you use the library tag, it must be the first tag in the mxml or fxg document.
- The library tag can contain one or more definition tags.
- The name attribute must be provided for the definition label as the "Class Name", and the definition label must have only one direct sub-tag as the base class of the defined class.
- The <FX: script/> and <FX: Metadata/> labels can also be used for classes defined using the definition label.
- The class defined by the definition label belongs to the default package and must be referenced using the namespace of mxml 2009. If the class name is myclass, use the class label in mxml as <FX: myclass/>.
- The Library and definition labels cannot be nested. That is, the library and definition labels cannot be used in the classes defined by definition.
In the following code, the myclass class and its subclass mysubclass are created using the library and definition labels:
<! -- MyApp. mxml --> <s: applicaiton xmlns: FX =...> <FX: Library> <! -- A class named myclass is defined here and inherited from the group class --> <FX: Definition name = "myclass"> <s: group> <s: rect width = "200" Height = "200"> <s: Fill... /> </S: rect> </S: group> </FX: Definition> <! -- A class named mysubclass is defined here, inherited from myclass class> <FX: definiton name = "mysubclass"> <FX: myclass/> </FX: definition> </FX: Library> <! -- Use the myclass and mysubclass classes --> <FX: myclass/> <FX: mysubclass/> </S: Application>
In addition, the instance of the class defined by definition cannot assign the ID attribute (for example, in the above Code, if the ID attribute is declared for the myclass instance, it will cause a compilation error ). This is essentially determined by the "private" of the library. In most cases, this label is used in fxg to define reusable graphic elements.
Tips
In fact, although we cannot assign the ID attribute to the instance of the class defined by definition, we can still get the reference of these classes by viewing the as code after flex compilation (using the-keep parameter) we can find that:All the classes defined by definition will eventually be converted to an independent ActionScript class named "mxml class name_definitionn. "..
For example, in the above example (mxml class named MyApp), myclass and mysubclass will eventually be converted to myapp_definition1 and myapp_definition2. After determining the actual class names of these classes, we can obtain and operate these classes through some system events, such:
<! -- Test. mxml --> <? XML version = "1.0" encoding = "UTF-8"?> <S: Application xmlns: FX = "... "xmlns: S = "... "> <FX: Library> <FX: Definition name =" myrect "> <s: group width =" 100 "Height =" 100 "> .... </S: group> </FX: Definition> </FX: Library> <FX: myrect creationcomplete = "myrect1_creationcomplete (event)"/> <FX: SCRIPT> <! [CDATA [import MX. Events. flexevent; private var myrect: test_definition1; <! -- Get instance reference through event --> protected function myrect1_creationcomplete (Event: flexevent): void {myrect = event.tar get as test_definition1; myrect. X = 200 ;}]]> </FX: SCRIPT> </S: Application>
<Private/>
The private tag is used to provide meta information of the mxml and fxg documents. The content in the tag is ignored by the compiler. However, you must ensure that the content is in a valid XML format. For example:
<fx:Private> <Author>Jinni Cao</Author> <Version>1.0</Version> <Site>http://www.SWFever.com</Site></fx:Private>
<Reparent/>
In the next chapter "improved view State Syntax", we will detail how to use the reparent label.
This chapter focuses on several new language-level labels added to mxml 2009. In the next chapter, we will introduce a feature greatly improved in flex 4: View state ).