2

Flex 的新States 将标记有includeIn/excludeFrom 的视觉项重新设置为父项。如果我有一个包含 5 个受状态控制的子/元素的组 (MainGroup),是否还有办法获得对 MainGroup 的子项的引用?mainGroup.numChildren 和 mainGroup.numElements 不起作用,因为孩子们重新成为父母。充其量,它们显示 1。

<s:states>
   <s:State name="view1State" />
   <s:State name="view2State" />
   <s:State name="view3State" />
   <s:State name="view4State" />
</s:states>

<s:Group id="mainGroup">
    <shipping:OrderShipping id="view1"
            includeIn="view1State" />
    <payment:OrderPayment id="view2"
            includeIn="view2State" />
    <verification:OrderVerification id="view3"
            includeIn="view3State" />
    <confirmation:OrderConfirmation id="view4"
            includeIn="view4State" />
</s:Group>
4

4 回答 4

2

如果您希望状态控制可见/includeinlayout 而不是父母身份,只需这样做

<s:states>
    <s:State name="view1State" />
    <s:State name="view2State" />
    <s:State name="view3State" />
    <s:State name="view4State" />
</s:states>
<s:Group id="mainGroup">
    <shipping:OrderShipping id="view1" 
        visible="false" 
        includeInLayout="false" 
        visible.view1State="true" 
        includeInLayout.view1State="true"/>
    <payment:OrderPayment id="view2" 
        visible="false" 
        includeInLayout="false" 
        visible.view2State="true" 
        includeInLayout.view2State="true"/>
    <verification:OrderVerification id="view3" 
        visible="false" 
        includeInLayout="false" 
        visible.view3State="true" 
        includeInLayout.view3State="true" />
    <confirmation:OrderConfirmation id="view4" 
        visible="false" 
        includeInLayout="false"
        visible.view4State="true" 
        includeInLayout.view4State="true"/>
</s:Group>
于 2011-08-17T21:44:33.653 回答
2

我在我的一个应用程序中遇到了类似的情况,这就是我完成事情的方式。我觉得有必要这么说,虽然这行得通,但我觉得它有点 hacky。考虑到这一点:

  1. 创建另一个状态,我们称之为cheating。将此状态设置为默认状态(将其放在您的states数组中。
  2. 更新所有视图以包含在此状态中。
    例如, <shipping:OrderShipping id="view1" includeIn="cheating,view1State" />
  3. Array创建一个名为的新类成员views
  4. 添加creationComplete事件处理程序。在处理程序中填充views对每个视图的引用。例如,views = [view1, view2, view3, view4];
  5. 将状态更改为您的“第一个”(非cheating)状态:setCurrentState("view1State");

毕竟views,无论您的应用程序当前处于哪种状态,您的数组都将引用每个视图。

于 2011-08-17T21:45:17.043 回答
1

我不确定底层状态实现是否将子组件删除/添加到组件或更改其可见性。

如果是前者,我会尝试这样的事情:

for (var x : int = 0; x<mainGroup.numChildren ; x++){
 var child : UIComponent = mainGroup.getChildAt(x);
 if(child.parent){
   // The child has a parent, so it must be displayed; do your processing 
 } else {
   // really this should never occur 
 }
}

如果这是可见性问题,请执行以下操作:

for (var x : int = 0; x<mainGroup.numChildren ; x++){
 var child : UIComponent = mainGroup.getChildAt(x);
 if(child.visible){
   // The child is displayed; do your processing 
 } else {
   // child isn't displayed
 }
}

我猜第一种方法是正确的。

[注意我在浏览器中编写了这段代码]

于 2011-08-17T21:23:48.343 回答
1

我得到了最好的答案!好吧,我会让你决定。只需对状态数组进行操作...

 for each (var viewState:State in mainDocument.states)
{
    var overrides:Array = viewState.overrides;

    for (var i:int = 0; i < overrides.length; i++)
    {
        var addItems:AddItems = overrides[i];
        if (addItems.destination === mainDocument.mainGroup)
            trace((addItems.items as UIComponent).name);
    }

}
于 2011-08-18T05:11:29.367 回答