Apache flex 为什么自定义组件中的XMLListCollection属性始终为空?

Apache flex 为什么自定义组件中的XMLListCollection属性始终为空?,apache-flex,flex3,custom-component,Apache Flex,Flex3,Custom Component,我编写了以下自定义组件SubNavBar.mxml: <?xml version="1.0" encoding="utf-8"?> <mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" height="100" width="300" creationComplete="init()"> <mx:Script> <![CDATA[ import mx.collections.XMLLis

我编写了以下自定义组件SubNavBar.mxml:

<?xml version="1.0" encoding="utf-8"?>
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" height="100" width="300"
 creationComplete="init()">

 <mx:Script>
  <![CDATA[
   import mx.collections.XMLListCollection;

   [Bindable] public var menuItems:XMLListCollection;

   private function init():void
   {
    trace("SubNav: config = "+menuItems);
   }


  ]]>
 </mx:Script>

 <mx:HBox y="30" id="menu">
  <mx:List dataProvider="{menuItems}"/>
 </mx:HBox>

</mx:Canvas>
我使用以下代码在父自定义组件中设置此组件:

<com:SubNavBar id="subNavMenu" menuItems="{subNavConfig}"
 x="10" y="-15">
</com:SubNavBar>
每当跟踪函数在init中运行时,属性menuItems都返回null。对于其他变量类型,比如Boolean或String,我似乎没有这个问题。这是由于XMLListCollection对象的大小造成的吗?如何使用XMLListCollection属性设置此SubNavBar自定义组件并将其绑定到组件中的控件


谢谢

也许我遗漏了什么,但是你在哪里设置{subNavConfig}

编辑:

这可能是因为它是如何铸造的…试着像

var listcol:XMLListCollection = new XMLListCollection(configXML.destination.(@mapID == mapID).subSections);

我测试了这段代码,一切似乎都对我有效。是否确实正确设置了subNavConfig变量

以下是我使用的客户端代码:

// Test.mxml

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" initialize="onInit()" xmlns:ns="comp.*">
  <mx:Script>
    <![CDATA[
      import mx.collections.XMLListCollection;

      [Bindable]
      public var bookCollection:XMLListCollection;

      public function onInit():void
      {
        var books:XML = <books>
                          <book publisher="Addison-Wesley" name="Design Patterns" />
                          <book publisher="Addison-Wesley" name="The Pragmatic Programmer" />
                          <book publisher="Addison-Wesley" name="Test Driven Development" />
                          <book publisher="Addison-Wesley" name="Refactoring to Patterns" />
                          <book publisher="O'Reilly Media" name="The Cathedral & the Bazaar" />
                          <book publisher="O'Reilly Media" name="Unit Test Frameworks" />
                        </books>;

        var booklist:XMLList = books.book;
        bookCollection = new XMLListCollection(booklist);
      }
    ]]>
  </mx:Script>

  <ns:SubNavBar id="fb" menuItems="{bookCollection}"/>
</mx:Application>
这是我得到的结果:

SubNav: config = <book publisher="Addison-Wesley" name="Design Patterns"/>
<book publisher="Addison-Wesley" name="The Pragmatic Programmer"/>
<book publisher="Addison-Wesley" name="Test Driven Development"/>
<book publisher="Addison-Wesley" name="Refactoring to Patterns"/>
<book publisher="O'Reilly Media" name="The Cathedral &amp; the Bazaar"/>
<book publisher="O'Reilly Media" name="Unit Test Frameworks"/>

谢谢你关注我的问题。我设置了一个新项目来测试您的代码,但仍然得到SubNav:config=null;但是,我向SubNav组件添加了一个DataGrid控件,它使用books XML变量中指定的数据进行呈现。SubNav组件似乎正在绑定XMLListCollection,但没有及时绑定SubNav的init中的跟踪函数。有什么想法吗?请仔细检查,您是否在main.xml中调用creationComplete上的onInit?实际上,我在初始化事件处理程序中调用onInit。我已经更新了答案以包含应用程序标记。creationComplete事件仅在创建组件及其所有子组件后触发,因此此时SubNavBar中的init函数已经执行。您可以找到有关在{subNavConfig}发生的顺序事件的更多信息。它是父自定义组件Destination.mxml的属性。当目标被实例化时,此属性在main.mxml中设置,例如Destination.subNavConfig=configXML.Destination@mapID==mapID.xml作为XMLListCollection。configXML是HTTPrequest对本地setup.xml文件的事件结果。