自定义 Flex 4 数据组件不会在 fx:声明标记内编译(并且不允许在标记外部编译)



我构建了一个广泛的库来呈现来自多个XML源的复杂数据,并将其中一些源抽象为仅扩展Object的类。作为一个实验,我开始扩展XMLListCollection,试图使数据处理实现更加集成和类似Flex,但到目前为止,mxmlc并没有与有用的消息合作。

生成错误的编译:

(fcsh) mxmlc  -use-network=true -omit-trace-statements=false -debug=true xmllist_test.mxml
fcsh: Assigned 1 as the compile target id
Loading configuration file /dev/Flex SDK/frameworks/flex-config.xml
Recompile: xmllist_test.mxml
Reason: The source file wasn't fully compiled.
Files changed: 0 Files affected: 1
xmllist_test.mxml(-1):  Error: Incorrect number of arguments.  Expected 1.
<?xml version="1.0" encoding="utf-8" ?>
(fcsh) 

有问题的 mxml,xmllist_test.mxml:

<?xml version="1.0" encoding="utf-8" ?>
<!-- xmllist_test -->
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
xmlns:mx="library://ns.adobe.com/flex/mx" 
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:maf="mafComponents.*"
creationComplete="makeItSo()"
backgroundColor="#FFFFFF">
<s:layout>
    <s:VerticalLayout/>
</s:layout>
<fx:Script>
<![CDATA[
import mafComponents.examples.*;
public var maf:XML = MafExamples.ghp010; // defined statically in the package
public function makeItSo():void
{
    trace('name', maf.@name);
}
]]>
</fx:Script>
<fx:Declarations>
    <!-- a comment -->
    <mx:XMLListCollection id="mafSource1" source="{maf.block}"/> <!-- no problem -->
    <maf:MafXMLListCollection id="mafSource2" metasource="maf}"/> <!-- won't compile -->
</fx:Declarations>
</s:Application>

我在上面的xmllist_test.mxml代码中包含一个使用 <mx:XMLListCollection> 工作的示例,但是由于存在我的自定义组件<maf:MafXMLListCollection>,整个编译失败。

我不知道如何解释编译器消息,考虑到xmllist_test.mxml(-1)似乎在计算行之前指示错误,以及 xml 声明行的报告。

我找不到任何关于如何以这种方式使用fx:Declarations的文档,尽管它被声明为非视觉组件保留的地方,我的是:

package mafComponents
{
    import mx.collections.*;
    /**
    * An extension of XMLListCollection that specifically serializes the "block" elements of a MAF, while processing other information as well.
    **/
    public class MafXMLListCollection extends XMLListCollection
    {
        public var maf:XML;
        public var mafXMLList:XMLList;
        public var name:String;
        private var _metasource:*;
        /**
        * Process the sequence of "blocks" in datasource to an XMLList to use as the parent class's "source" attribute
        **/
        public function set metasource(datasource:*):void
        {
            // multiple options for source, get it to the point of an XML with "maf" at the root
            if (datasource is XML)
            {
                maf = datasource as XML;
            }
            mafXMLList = maf.block; // this expression returns an XMLList
            name = 'MafXMLListCollection_' + maf.@name;
            source = mafXMLList;
            // do other stuff with the data structure here
            // ...
        }
        /**
         * @private
        **/
        public function get metasource():* { return _metasource; }
        public function MafXMLListCollection(datasource:*)
        {
            super();
            metasource = datasource;
            // make the main list from the sequence of "block" sections in the XML
            trace(name + '.length: ' + length);
        }
    }
}

尽管存在此问题,但我可以通过 <fx:Script> 标记中的 actionscript 使用自定义组件,而不会出错。我只是不明白MXML编译失败是因为我犯了语法错误,还是我的误解是在哲学层面上,我尝试了一些没有实现的东西。

提前感谢您就该主题提供的任何观点。

1)由于MafXMLListCollection没有实现IVisualElement,因此它不能包含在可视元素声明中(因此需要标记的警告)。

2) 由于 MafXMLListCollection 具有构造函数的参数,因此不能使用 MXML 格式声明。 您必须在 <脚本> 块中创建变量:

public var mafSource2:MafXMLListCollection = new MafXMLListCollection(maf);

或者,您可以将 MafXMLListCollection 更改为具有默认构造函数,并使用元源属性(已在 MXML 中执行此操作)。

最新更新