从 MXML 初始化集合 [英] Initializing a collection from MXML
问题描述
先生们,
如何在 Flex/Actionscript 中从 MXML 初始化集合实例?
How does one initialize a collection instance from MXML in Flex/Actionscript?
背景
假设:
- 我有三个数字名称列表对.
- 每个列表的内容从不更改,但我可能想添加新的将来列出.
- 用户可以选择哪个列表使用.
我知道怎么做使用 MXML 来定义/初始化一个Flex UI 组件,但这就是我的 XML 经验的全部内容.
I know how to use MXML to define/initialize a Flex UI component, but that is the full extent of my XML experience.
问题
如何编写 XML 声明和 Actionscript 类的组合,以便我可以从 XML 初始化我的三个列表中的每一个?
How do I write a combination of XML declarations and Actionscript classes such that I can initialize each of my three lists from XML?
请注意,我不会尝试使用各种数字-名称对填充 Flex UI 元素(例如 DataGrid).我只是想将数据读入一个普通的旧香草集合.(一旦我的集合被初始化,我就可以在空闲时填充 DataGrids 或任何东西.)我找不到任何关于如何解决这个超级简单案例的文档.文档都假设我正在尝试做一些更复杂的事情,例如访问远程数据库,这会极大地混淆问题.
Please note that I am not trying to populate a Flex UI element (such as a DataGrid) with the various number-name pairs. I'm just trying to read the data into a plain old vanilla collection. (Once I've got my collections initialized, I can populate DataGrids or whatever at my leisure.) I can't find any documentation of how to address this super-simple case. The documentation all assumes that I'm trying to do something much more complicated, such as accessing a remote database, which confuses the issue tremendously.
谢谢!:-)
德克萨斯州吉姆普拉蒙登
Jim Plamondon, Texas
推荐答案
有几种方法可以做到这一点:
There's a few ways you can do this:
样本数据集:
<?xml version="1.0" encoding="UTF-8"?>
<events type="array">
<event>
<date>12-50-99</date>
<title>Event A</title>
<location>San Diego, CA</location>
</event>
<event>
<date>12-50-99</date>
<title>Event B</title>
<location>Healdsburg, CA</location>
</event>
</events>
弹性 4
XML 声明
以下是将数据导入 XML 或 ArrayCollection 的 3 种方法,您可以将这两种方法传递给您的数据提供者.
Flex 4
XML Declarations
Below are 3 ways to get data into XML or an ArrayCollection, both of which you can pass to your data provider.
<?xml version="1.0" encoding="utf-8"?>
<s:Application
xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx">
<fx:Declarations>
<fx:XML id="data" source="events.xml"/>
<fx:XML id="data2">
<event>
<date>12-50-99</date>
<title>Event A</title>
<location>San Diego, CA</location>
</event>
<event>
<date>12-50-99</date>
<title>Event B</title>
<location>Healdsburg, CA</location>
</event>
</fx:XML>
<fx:Declarations>
<mx:ArrayCollection id="data3">
<fx:Object date="12-50-99" title="Event A" location="San Diego, CA"/>
<fx:Object date="12-50-99" title="Event B" location="Healdsburg, CA"/>
</mx:ArrayCollection>
</fx:Declarations>
</fx:Declarations>
<!-- then your views -->
<mx:Button/>
</s:Application>
Flex 3 的工作原理相同,但您只需删除 <fx:Declarations/>
标签.
Flex 3 works the same, but you just remove the <fx:Declarations/>
tags.
如果您希望数据是动态的,我只需在您视图中的 <mx:Script/>
块中为您的 ArrayCollection 或 XMLListCollection 创建一个属性,例如 [Bindable] public var myData:ArrayCollection;
,并通过 XML 将数据加载到其中.通过将您的数据保持在外部(而不是将数据硬编码/嵌入到 MXML 中),您无需重新编译,而且添加越来越多的数据也变得更加容易.
If you want the data to be dynamic, I would just create a property for your ArrayCollection or XMLListCollection in the <mx:Script/>
block in your view, say [Bindable] public var myData:ArrayCollection;
, and load the data into that via XML. By keeping your data external (not hardcoding/embedding data into MXML), you don't have to recompile, and it's easier to add more and more.
为此,您需要使用 URLRequest 或 HTTPService.HTTPService 基本上是一个围绕 URLRequest 的 MXML 包装器.
In order to do that, you'd either need to use URLRequest or HTTPService. HTTPService is basically an MXML wrapper around the URLRequest.
类似于这个伪代码:
<?xml version="1.0" encoding="utf-8"?>
<s:Application
xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
initialize="loadData()">
<fx:Script>
import mx.collections.*;
[Bindable] public var list1:IList;
[Bindable] public var list2:IList;
[Bindable] public var list3:IList;
public function loadData():void
{
eventsService.load(); // loads, may take a few frames/seconds
}
public function updateData(result:Object, property:String):void
{
// this["list1"] = xml;
this[property] = new XMLListCollection(result);
}
</fx:Script>
<fx:Declarations>
<mx:HTTPService id="eventsService"
url="events.xml"
resultFormat="e4x"
result="updateData(event.result, 'list1');"
fault="trace('eventsService Error');"/>
</fx:Declarations>
<!-- then your views -->
<mx:List dataProvider="{list1}"/>
</s:Application>
让我知道这是否有效.
这篇关于从 MXML 初始化集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!