首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >为什么我的自定义组件中的XMLListCollection属性总是为空?

为什么我的自定义组件中的XMLListCollection属性总是为空?
EN

Stack Overflow用户
提问于 2009-12-12 06:56:44
回答 2查看 277关注 0票数 0

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

代码语言:javascript
运行
复制
<?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>

我使用以下代码在父自定义组件中设置此组件:

代码语言:javascript
运行
复制
<com:SubNavBar id="subNavMenu" menuItems="{subNavConfig}"
 x="10" y="-15">
</com:SubNavBar>

只要在init()中运行trace函数,属性menuItems就会返回null。对于其他变量类型,例如Boolean或String,我似乎没有这个问题。这是由于XMLListCollection对象的大小造成的吗?如何使用XMLListCollection属性设置此SubNavBar自定义组件并将其绑定到组件中的控件?

谢谢!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2009-12-12 08:03:58

我测试了这段代码,似乎一切都对我有用。您确定正确设置了subNavConfig变量吗?

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

代码语言:javascript
运行
复制
// 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>

下面是我得到的输出:

代码语言:javascript
运行
复制
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"/>
票数 0
EN

Stack Overflow用户

发布于 2009-12-12 07:10:21

也许我遗漏了一些东西,但您将{subNavConfig}设置在哪里?

编辑:

这可能是因为它是如何被casted...try的,比如...

代码语言:javascript
运行
复制
var listcol:XMLListCollection = new XMLListCollection(configXML.destination.(@mapID == mapID).subSections);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1891423

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档