OK, can someone please tell me the REAL syntax for doing state开发者_如何学JAVAs in mxml in Flex 4???
I've seen both of the following (and implemented both) and don't know which way is "right" or preferred.
In the <fx:Declarations>
<fx:Declarations>
...
<s:State name="state1" />
<s:State name="state2" />
<s:State name="state3" />
</fx:Declarations>
<s:Button id="button1" ... />
...
<more components>
OR
"Top-most" level in a component
<s:states>
<s:State name="state1" />
<s:State name="state2" />
<s:State name="state3" />
</s:states>
<s:Button id="button1" ... />
...
<more components>
Probably the second one; but It depends what you're trying to do.
This Syntax:
<fx:Declarations>
...
<s:State name="state1" />
<s:State name="state2" />
<s:State name="state3" />
</fx:Declarations>
Creates three variables inside the component of the class State. It would be the same as doing something like this in ActionScript:
var myState1 : State = new State();
var myState1 : State = new State();
var myState1 : State = new State();
When creating states on a component, you probably want to do more than just create a state. You want to assign the state as a state of the current component.
That is what your second syntax actually does:
<s:states>
<s:State name="state1" />
<s:State name="state2" />
<s:State name="state3" />
</s:states>
This creates three state variables as an array, and assigns them to the state property of the top level component. Conceptually like this in ActionScript:
var myState1 : State = new State();
var myState1 : State = new State();
var myState1 : State = new State();
this.states = new Array();
this.states.push(myState1);
this.states.push(myState2);
this.states.push(myState3);
It is entirely possible that the compiler does some magic on the first syntax to create those state instance and assign them to the states array, but I'm not sure. I've never seen anyone use that syntax before. I would expect both approach to compile. I would only expect the second approach to actually create states on a component that you can switch between.
精彩评论