Gentlepersons,
Gentlepersons,
一个人如何从MXML中的Flex /动作脚本初始化一个集合实例?
How does one initialize a collection instance from MXML in Flex/Actionscript?
背景
让我们假设:
我已经得到了数名三个列表 对。 在每个列表中的内容从来没有 变化,但我可能要添加新的 在今后的列表中。 用户可以选择列表 使用。 I've got three lists of number-name pairs. The contents of each list never changes, but I may want to add new lists in future. The user can choose which list to use.我知道如何 用MXML定义/初始化 柔性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)与各数名对。我只是想读取数据到一个普通的老香草集合。 (一旦我有我的集合初始化的,我可以填充在我的闲暇DataGrid中或什么的。)我无法找到如何解决这个超级简单的情况下,任何文件。文档的一切假设我试图做一些事情要复杂得多,比如访问远程数据库,从而极大地混淆了问题。
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>
下面是3种方式来获得数据转换成XML或一个ArrayCollection,这两者可以传递给你的数据提供者。
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的工作原理是相同的,但你只是删除&LT; FX:声明/&GT;
标签
如果你希望数据是动态的,我只是想创建一个属性为您的ArrayCollection或的XMLListCollection在&LT; MX:脚本/&GT;
在你看来块,说 [可绑定]公共变种的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.
事情是这样的伪code:
Something like this pseudo code:
<?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>
让我知道,如果工程。