Join David Gassner for an in-depth discussion in this video Understanding the new MXML namespaces, part of Flash Builder 4 and Flex 4 New Features.
One of the first things you'll notice, if you are a Flex 3 developer, is that when you create your first Flex 4 project and application, that the number and syntax of the namespace prefixes are significantly different than in Flex 3. In Flex 3, we only used a single namespace prefix. It was named mx, and it was associated with a single MXML namespace. In a Flex 4 application, by default, there are three namespaces. They have prefixes of fx, s, and mx.
The fx prefix is associated with compiler tags, such as the <fx: Declarations> element that you see here, and also with tags that instantiate ActionScript classes that are top-level classes of Flash Player. For example, if I were to declare an instance of the String class, that would be associated with the fx tag. In Flex 4, if you want to declare an instance of a non-visual class, such as a String, you must place it inside the <fx: Declarations> tag.
So, I'll place the cursor inside the Declarations tag and type in a less than character to start a tag, and then I will type 'str.' There is the <fx: String> tag. Notice, again, that it's associated with the fx prefix. I'll close the tag and type in a string of 'Hello World.' So, I have now declared a Public variable where the data type is String and the value is Hello World. I will add an id to the tag so that I can address the variable in the rest of my application.
I will name it helloString. Then I will place the cursor after the declarations element. Now I'll create a label control. A label control behaves pretty much the same ways as it did in Flex 3, but it's actually a new component, a new class that's a member of a component set called the Spark components. I'll describe the differences between the Spark components and the older MX components in other videos, but for the moment, I want to focus on the namespaces. I'll type in a less than character and then 'la' and that will take me to the label component.
Notice, in the Code Assist list that drops down, that there are two instances of the Label component, one with the prefix of s, for Spark, and one with a prefix of mx. You should always use the Spark version of the label component in a Flex 4 application. It supports all of the new text rendering features of Flash Player 10. I will set the text property of the Label component using a binding expression to helloString. And just as I would in Flex 3, I will set a fontSize and a fontWeight. Assigning styles through attributes hasn't changed.
Now, I'll save and run the application and you'll see that the default appearance of a Flex 4 application is much plainer, or simpler, than in Flex 3. By default, a Flex 4 application uses a very simple white background and places its objects on the screen using basic layout. Basic layout in Flex 4 is like Absolute layout in Flex 3. Basic layout means that you place objects relative to the top-left corner of the application, or container, or you use constraint properties, such as top, bottom, left, right, horizontal, center or vertical center.
So, back to the subject of namespaces. The s prefix means that a visual component is a member of the Spark component set. So, both the label and the application are members of that component set. Tags that you use to declare instances of top-level classes, such as String, Number, Boolean and other data types, those are members of the FX prefix. And if you want to use one of the older MX space controls, such as a DataGrid, you will still use the MX prefix. I will type a less than character and then data, and I will press Ctrl+Shift+Space to look at the Code Assist list, and there is the MX DataGrid.
So, again, three prefixes are now used in Flex 4 applications, fx for compiler tags and top-level classes, s for the new Spark component set, and mx for the older MX components.
- Understanding MXML namespaces
- Moving classes and components between packages
- Managing templates for new MXML and ActionScript files
- Exploring MXML language changes
- Working with the Spark component framework
- Adding visual controls with CSS, MXML graphics, and custom Spark skins
- Displaying images and dynamic data