【问题标题】:Why are my XMLListCollection properties in my custom components always null?为什么我的自定义组件中的 XMLListCollection 属性始终为空?
【发布时间】:2009-12-11 22:56:43
【问题描述】:

我编写了以下自定义组件,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 自定义组件并将其绑定到组件中的控件?

谢谢!

【问题讨论】:

    标签: apache-flex flex3 custom-component


    【解决方案1】:

    我测试了这段代码,一切似乎都适合我。您确定您正确设置了 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() 中的 trace() 函数。有什么想法吗?
    • 只是为了仔细检查,您是否在 main.xml 中的 creationComplete 上调用 onInit()?
    • 其实我是在初始化事件处理程序中调用 onInit() 。我已经更新了答案以包含 Application 标签。 creationComplete 事件仅在创建组件及其所有子组件后触发,因此到那时 SubNavBar 中的 init() 函数已经执行。您可以在livedocs.adobe.com/flex/3/html/… 找到有关订单事件的更多信息
    【解决方案2】:

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

    编辑:

    这可能是因为它的投射方式...尝试类似...

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

    【讨论】:

    • {subNavConfig} 是父自定义组件 Destination.mxml 的属性。当实例化 Destination 时,此属性在 main.mxml 中设置,例如“destination.subNavConfig = configXML.destination.(@mapID == mapID).subSections 作为 XMLListCollection”。 configXML 是对本地 setup.xml 文件的 HTTP 请求的事件结果。
    猜你喜欢
    • 2021-06-30
    • 2015-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-12
    • 1970-01-01
    • 2018-01-27
    • 2022-01-04
    相关资源
    最近更新 更多