简体   繁体   English

如何添加Flex 4 Spark List Item自定义属性

[英]How to add Flex 4 spark List Item custom properties

I'm trying to make a simple flash application providing interface for taking tests as a high school assignment. 我正在尝试制作一个简单的Flash应用程序,以提供将考试作为高中作业的界面。 One of the requirements is to use an XML file as data source. 要求之一是使用XML文件作为数据源。
Now, having a List component bound to the XML file with questions consisting of data such as question body, question type (ie. single choice, multiple choice, open, image etc.) and possible answers (where applicable), I was wondering if I could add some additional data (and what is the best possible way to do so) to each question upon its transfer to the List component. 现在,将一个List组件绑定到XML文件,其中的问题由诸如问题正文,问题类型(即单选,多选,开放,图像等)和可能的答案(如适用)之类的数据组成,我想知道是否在将每个问题转移到List组件后,我可以向每个问题添加一些其他数据(以及这样做的最佳方法是什么)。
I am trying to achieve two main goals with this: firstly, to mark the questions to which an answer has already been given, like with such code in ItemRenderer class: 我正在尝试达到以下两个主要目标:首先,标记已经给出答案的问题,例如ItemRenderer类中的此类代码:

<s:Label color="{data.color}" text="{data.label}"/>

where data.color would be set whenever the user gives an answer to a question. 当用户给出问题答案时,将设置data.color。
Secondly, while at it, I thought of such possibility as a great way to store answers given to particular questions. 其次,在讨论过程中,我认为这种可能性是存储针对特定问题的答案的好方法。 In this case, the Class of the answer object would have been Object, since there has to be many type of questions (where the answer could also be a Bitmap for example). 在这种情况下,答案对象的类将是对象,因为必须有多种类型的问题(例如答案也可以是位图)。

This is a question of both how to do it and if it seems a good idea at all (and if no, if there is a better way?), because I am quite new to the whole Flash Builder and Flex thing and I am not really accustomed to all the possibilities and best practices. 这是一个问题,既要怎么做,又是否一个好主意(如果没有,是否有更好的方法?),因为我对整个Flash Builder和Flex都是陌生的,但我不是真正习惯了所有可能性和最佳实践。

Thanks! 谢谢!

It is considered best practice to assign your XML data results to ValueObjects, you could either add additional data directly in the ValueObject class or use another Class to specifically add logic to your VOs, this in order not to mix logic and data within the same class. 将XML数据结果分配给ValueObject被认为是最佳实践,您可以直接在ValueObject类中添加其他数据,也可以使用其他类专门为VO添加逻辑,以免在同一类中混合逻辑和数据。

If you're not familiar with Flex/FlashBuilder , have a look at the following tutorial, here's a link referring to VOs 如果您不熟悉Flex / FlashBuilder,请查看以下教程,这里是指向VO的链接
http://www.adobe.com/devnet/flex/videotraining/exercises/ex2_08.html#b http://www.adobe.com/devnet/flex/videotraining/exercises/ex2_08.html#b

I prefer using something like a Presentation Model for each of the items in the list. 我更喜欢为列表中的每个项目使用类似Presentation Model的外观。

The idea is that you compose a class with the XML data (model data) AND the properties that are more germain to the view. 这个想法是您用XML数据(模型数据)和对视图更为重要的属性组成一个类。 In other words, you map the XML data to a higher-level type and push that into the List. 换句话说,您将XML数据映射到更高级别的类型,并将其推入List。 Something like this, where label delegates to the model, but color is something you add in your UI layer. 像这样的东西,其中label委托给模型,但是color是您在UI层中添加的东西。

public class QuestionPM {
    private _model:XML;
    public function QuestionPM(model:XML) {
        _model = model;
    }

    public function get label():String { return _model.label; }
    public var color:String;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM