开发者

Different behaviour between Flex mx:XMLList and fx:XMLList

Having this code in a flex3 project worked (note the enabled property on the sixth item):

    <mx:XMLList id="items">
            <menuitem label="file">
                <menuitem label="file.new" data="New"/>
                <menuitem label="file.saveAs" data="SaveAs"/>
                <menuitem label="file.open" data="Open"/>
                <menuitem label="file.close" data="Close"/>
            </menuitem>
            <menuitem label="business">
                <menuitem label="business.add" data="AddBusiness"/>
                <menuitem label="business.delete" enabled="{someproperty.selected}" data="DeleteBusiness"/>
            </menuitem>
            <menuitem label="help">
                <menuitem label="help.about" data="About" />
            </menuitem>
        </mx:XMLList>

However fx:XMLList can't handle binding in that way. This code gives a compilation error:

        <fx:XMLList id="items开发者_JAVA百科">
            <menuitem label="file">
                <menuitem label="file.new" data="New"/>
                <menuitem label="file.saveAs" data="SaveAs"/>
                <menuitem label="file.open" data="Open"/>
                <menuitem label="file.close" data="Close"/>
            </menuitem>
            <menuitem label="business">
                <menuitem label="business.add" data="AddBusiness"/>
                <menuitem label="business.delete" enabled="{someproperty.selected}" data="DeleteBusiness"/>
            </menuitem>
            <menuitem label="help">
                <menuitem label="help.about" data="About" />
            </menuitem>
        </fx:XMLList>

1120: Access of undefined property items.


I suppose you are compiling your project in strict mode and that you are using the items XMLList directly as dataProvider for some component (at least it was the only way I've found to reproduce the problem), like this:

<s:List>
    <s:dataProvider>
        <s:XMLListCollection>
            <fx:XMLList id="items">
                <menuitem label="file">
                    <menuitem label="file.new" data="New"/>
                    <menuitem label="file.saveAs" data="SaveAs"/>
                    <menuitem label="file.open" data="Open"/>
                    <menuitem label="file.close" data="Close"/>
                </menuitem>
                <menuitem label="business">
                    <menuitem label="business.add" data="AddBusiness"/>
                    <menuitem label="business.delete" enabled="{someproperty.selected}" data="DeleteBusiness"/>
                </menuitem>
                <menuitem label="help">
                    <menuitem label="help.about" data="About"/>
                </menuitem>
            </fx:XMLList>
        </s:XMLListCollection>
    </s:dataProvider>
</s:List>

This reproduces the same error (#1120) you mentioned. In order to fix this, you have to declare the XMLList prior to its container (in the example it's a List), in the <fx:Declarations> block. This way you are assuring that the items reference will exist before the container is created, which is what strict mode verifies during compilation.

<fx:Declarations>
    <fx:XMLList id="items">
        <menuitem label="file">
            <menuitem label="file.new" data="New"/>
            <menuitem label="file.saveAs" data="SaveAs"/>
            <menuitem label="file.open" data="Open"/>
            <menuitem label="file.close" data="Close"/>
        </menuitem>
        <menuitem label="business">
            <menuitem label="business.add" data="AddBusiness"/>
            <menuitem label="business.delete" enabled="{someproperty.selected}" data="DeleteBusiness"/>
        </menuitem>
        <menuitem label="help">
            <menuitem label="help.about" data="About"/>
        </menuitem>
    </fx:XMLList>
</fx:Declarations>

<s:List>
    <s:dataProvider>
        <s:XMLListCollection source="{ items }"/>
    </s:dataProvider>
</s:List>

Update: This will also generate the #1120 error if you don't have the compiler's strict mode option enabled. Just declare the XMLList in the <fx:Declarations> block and it should work fine.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜