为什么我的自定义组件中的XMLListCollection属性始终为null?

时间:2009-12-11 22:56:44

标签: 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.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>

每当trace函数在init()中运行时,属性menuItems都会返回null。我似乎没有其他变量类型的问题,如布尔值或字符串。这是由于XMLListCollection对象的大小?如何使用XMLListCollection属性设置此SubNavBar自定义组件并将其绑定到组件中的控件?

谢谢!

2 个答案:

答案 0 :(得分:0)

也许我错过了什么,但你在哪里设置{subNavConfig}?

修改

可能是因为它是如何被铸造的......尝试类似......

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

答案 1 :(得分:0)

我测试了这段代码,一切似乎对我有用。您确定要正确设置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"/>