I’m fairly new to ActionScript/Flex so I’m not entirely sure if this is even possible.
Basically I have the following block repeating several times in my code:
<s:TextInput .. \>
<s:BitmapImage .. \>
What I’m trying to do is create an ActionScript custom component so I can replace the above block everywhere in my code with:
<MyBlock\>
My best guess is I have to do this by extending spark.application?
What I have so far:
package MyPackage
{
import spark.components.Application;
public class MyBlock extends Application
{
..
}
..
}
I am completely at a loss as to how to combine two existing components into a new custom one, if it is even possible.
How exactly should I proceed next? Any help would be appreciated.
Thanks,
It is so much easier than that: for this use case you should simply extend
Group. And to make things easier, write your composed component in MXML.Create a new file
MyBlock.mxml(for instance incom/mydomain/components) and add the following code:Now simply use this component:
where the
cnamespace is defined asxmlns:c="com.mydomain.components.*"at the root node of your document using this class. For example:Now suppose you want to have a different text in each block, you’ll have to expose a property. To do this, lets add a
labelproperty toMyBlock:To make the
TextInputshow what’s in that variable whenever it changes, we use binding like so:The final component would look something like this:
Now you can create multiple
MyBlockinstances with different texts:Note that if your regular use of MyBlock is more in a list-like fashion, you may want to consider using a
Listcomponent with a custom ItemRenderer, rather then usingMyBlockover and over again.