I build a swc file by Flash CS5 contains some interface component like TextInput, Label.
And then I use it in a flex program.
But I meet the problem when I want use flex repeater for this component.
Following is the a component defined by myself in swc file using Flash CS.
package {
import fl.controls.TextInput;
......
public dynamic class MyWindow extends UIMovieClip {
public var txt1 : TextInput;
......
}
}
}
Then I use it in my flex program like this:
<local:MyWindow id="myWindow"/>
<fx:Script>
<![CDATA[
......
private function Init() : void {
myWindow.txt1.text = "myText";
}
......
]]>
</fx:Script>
it works well.
But how can I use txt1 in mxml directly? like this:
<local:MyWindow id="myWindow" txt1.text="myText"/>
I know it doesn't work, but I want use repeater to create some similar MyWindow, it need bind the dataProvider. I wrote flex code like this:
<mx:VBox>
<mx:Repeater x="10" y="10" id="multiWindow">
<local:MyWindow txt1.text="{multiWindow.currentItem}"/>
</mx:Repeater>
</mx:VBox>
But it can't work.
Does anyone know how to make it work? Thanks.
=================================================================================
Update code, multiWindow complete code is :
package {
import fl.controls.TextInput;
import mx.flash.UIMovieClip;
import flash.display.DisplayObject;
import flash.events.EventDispatcher;
import flash.display.Sprite;
import flash.display.InteractiveObject;
import flash.display.MovieClip;
import flash.display.DisplayObjectContainer;
public dynamic class MyWindow extends UIMovieClip {
public var txt1 : TextInput;
public var txt2 : TextInput;
public var txt3 : TextInput;
}
}
Any component that you want to use withing an MX container needs to implement IUIComponent. I believe that Flash has a built in base class that you can extend for use with Flex, but you can also just do something like this:
<mx:VBox>
<mx:Repeater x="10" y="10" id="multiWindow">
<mx:UIComponent>
<local:MyWindow txt1.text="{multiWindow.currentItem}"/>
</mx:UIComponent>
</mx:Repeater>
</mx:VBox>
Note that if you haven't given thought to the Flex Component life cycle and layout system, it might not play well.
Related
I'm writing a mobile app in Flex with FlashDevelop and using Flash CS6 to create the visual assets. I created a symbol in Flash CS6 and exported it as AC3 into an SWC, which I imported into my FlashDevelop project. I then created a class for it, like so:
package com
{
import flash.display.Sprite;
public class volmeter_class extends Sprite
{
private var design:volmeter;
public function volmeter_class()
{
trace("I'm a MySymbol instance called", name);
design = new volmeter();
addChild(design);
}
}
}
I'm now seeking to display the symbol I created on a page of my app. To do this, I suspect I am expected to somehow import it in my MXML, but I have no idea how to do so. Can someone give me a few tips on how to get started doing this?
In Flash Pro, assure AS Linkage is specified for the symbol.
Using the published SWC from Flash Pro in the ActionScript Build Path of a project, the symbol may be instantiated by AS Linkage. Or, symbols may be embedded from the published SWF of Flash Pro.
Pure ActionScript example:
package
{
import flash.display.Sprite;
public class AppExample extends Sprite
{
public function AppExample()
{
var exampleSymbol:ExampleSymbol = new ExampleSymbol();
addChild(exampleSymbol);
}
}
}
Flex MXML example:
<?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"
creationComplete="creationCompleteHandler(event)">
<fx:Script>
<![CDATA[
import mx.events.FlexEvent;
protected function creationCompleteHandler(event:FlexEvent):void
{
var exampleSymbol:ExampleSymbol = new ExampleSymbol();
symbol.addChild(exampleSymbol);
}
]]>
</fx:Script>
<s:SpriteVisualElement id="symbol" />
</s:Application>
Another approach would be to embed the symbol by linking a SWF from Flash Pro.
Flex MXML embed example:
<?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">
<fx:Script>
<![CDATA[
[Embed('assets/library.swf', symbol = 'ExampleSymbol')]
[Bindable]
public static var ExampleSymbolClass:Class;
]]>
</fx:Script>
<s:Image source="{ExampleSymbolClass}" />
</s:Application>
I have a custom TitleWindow component (written in ActionScript) that extends spark.componenets.TitleWindow
I want to define some controls to be in the control bar but I don't have all controls at the creation stage. Also, this custom component is the base for other components which need other controls in the control bar.
Is there a way to add controls to the controlBarContent in ActionScript at run time?
Maybe something like the following? (this obviously didn't work)
controlBarContent = [];
.
.
.
controlBarContent.push(new Button());
In general, look out for
addElement()
or
addChild()
methods.
addElement() adds other UI components as sub-elements of other components.
This might be of interest. Finnaly, Adobe provides good help here: 'Working with components'.
UPDATE-1
Sorry, my fault. This works for me:
<s:Panel creationComplete="init();" id='p' controlBarVisible="true" >
<s:controlBarContent>
<!-- will show controls -->
<s:Button label="dd">
</s:Button>
</s:controlBarContent>
<fx:Script>
<![CDATA[
import mx.controls.Button;
import spark.components.Button;
private function init(): void {
var s:spark.components.Label = new spark.components.Label();
s.text = 'My Label';
s.width = 200;
var a:Array = new Array();
a.push( s );
p.controlBarVisible = false;
p.controlBarContent = a;
p.controlBarVisible = true;
p.invalidateDisplayList();
}
]]>
</fx:Script>
</s:Panel>
The concept of this seems easy, but I'm having trouble getting it right and can't find anything to help me on this.
I have a panel I need to perform a drag and drop operation on, but I only want to perform that if the user mouses down on a particular area of the panel. I can add an Icon to the panel by doing this:
[Embed("/img/icon.png")]
[Bindable]
public var dragIcon:Class;
newPanel.titleIcon = dragIcon;
But what I really want to add is a box, which I can then add my listeners to for the drag and mouse down like I do on some canvases created in actionscript like so
var tempBox:Box = new Box;
tempBox.x=0;
tempBox.y=0;
tempBox.width = 20;
tempBox.height = 44;
tempBox.setStyle("horizontalAlign","center");
tempBox.setStyle("verticalAlign","middle");
tempBox.addEventListener(MouseEvent.ROLL_OVER,over);
tempBox.addEventListener(MouseEvent.ROLL_OUT,out);
tempBox.addEventListener(MouseEvent.MOUSE_DOWN,mouseDownAnswer);
var tempImg:Image = new Image();
tempImg.source = grabbableItem;
tempBox.addChild(tempImg);
myCanvas.addChild(tempBox);
So what do I need to do to use that tempBox and turn it into a class to be used as my panels titleIcon?
Edit 12/29/09:
So I came up with something where I'm extending the panel class (shown below) but all this is really doing is covering up the icon with something I can access publicly. I'm sure there's a better way out there right?
package custClass
{
import mx.containers.Box;
import mx.containers.Panel;
import mx.controls.Image;
public class DragPanel extends Panel
{
[Bindable] public var iconBox:Box = new Box();
[Embed("../img/doc_page.png")] [Bindable] public var grabbableItem:Class;
public function DragPanel()
{
super();
}
override protected function createChildren():void{
super.createChildren();
iconBox.x = 10
iconBox.y = 4
iconBox.width = 20;
iconBox.height = 20;
iconBox.setStyle("horizontalAlign","center");
iconBox.setStyle("verticalAlign","middle");
iconBox.setStyle("borderStyle","solid");
iconBox.setStyle("backgroundColor",0x000000);
var tempImg:Image = new Image();
tempImg.source = grabbableItem;
iconBox.addChild(tempImg);
this.rawChildren.addChild(iconBox);
}
}
}
EDIT 1/7/10 (or 16 according to my windows mobile phones text messages):
Using Chaims help from below here is my new answer.
Create a box mxml component like Chaim says but also add the following script block to it.
<mx:Script>
<![CDATA[
import mx.core.Application;
[Embed("/img/doc_page.png")]
[Bindable]
public var grabbableItem:Class;
public function init():void{
this.addEventListener(MouseEvent.MOUSE_DOWN,Application.application.mouseDownSection);
this.addEventListener(MouseEvent.ROLL_OVER,Application.application.over);
this.addEventListener(MouseEvent.ROLL_OUT,Application.application.out);
}
]]>
</mx:Script>
This adds in all the event listeners I want on the Box that will be used as my icon. Now just add the box as an Icon and it's good to go.
panel.titleIcon = DraggableBox;
I guess since it's a separate mxml component it is now a class, though I don't think I understand why.
The Panel expecting titleIcon property value to be a IFactory and create an instance by himself.
Make your box a component (lets name it DraggableBox.mxml):
<?xml version="1.0" encoding="utf-8"?>
<mx:Box xmlns:mx="http://www.adobe.com/2006/mxml"
x="0" y="0" width="20" height="44"
horizontalAlign="center" verticalAlign="middle">
<mx:Image source="{grabbableItem}"/>
</mx:Box>
And assign it to titleIcon:
<mx:Panel titleIcon="{DraggableBox}" >
...
</mx:Panel>
If you want do it in ActionScript use ClassFactory:
panel.titleIcon = new ClassFactory(DraggableBox);
In my flex app I have various custom components done with mxml or actionscript.
I want all of them to extend a base-class where I can define properties/event listeners etc.
Can someone give me an example how to create that base class and how I can extend it in mxml and actionscript components?
Maybe you could write a common interface for your components, just with the methods they need to implement
public interface ICustomComponent {
function doSomething():void;
// more methods here
}
And then in your AS components you just implement the ICustomComponent interface (or however you named it)
public class CustomButton extends Button implements ICustomComponent {
public function doSomething():void {
}
}
You can do this in MXML components too :
<mx:Button xmlns:mx="http://www.adobe.com/2006/mxml"
implements="ICustomComponent">
<mx:Script>
<![CDATA[
public function doSomething():void {
// blah blah
}
]]>
</mx:Script>
</mx:Button>
Just an idea. Hope it helps
Cheers
Creating the base class:
ActionScript
In BaseClass.as:
public class BaseClass
{
}
Extending from the base class:
ActionScript
public class SubClass extends BaseClass
{
}
MXML
In a file called SubClass.mxml:
<ns:BaseClass xmlns:ns="path.to.base.*">
</ns:BaseClass>
In the example below the component extends Form to create an address form .
Instead of form you can extend your own component .
When using actionscript i would suggest investigating flex components lifecycle for best performance :
http://livedocs.adobe.com/flex/3/html/help.html?content=ascomponents_advanced_2.html
mx:Form xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:MyComp="*"
<mx:FormItem label="NameField">
<mx:TextInput/>
</mx:FormItem>
<mx:FormItem label="Street">
<mx:TextInput/>
</mx:FormItem>
<mx:FormItem label="City" >
<mx:TextInput/>
</mx:FormItem>
<mx:FormItem label="State" >
<MyComp:StateComboBox/>
</mx:FormItem>
The following application file references the AddressForm component in the
AddressForm tag:
mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:MyComp="*"
<MyComp:AddressForm/>
/mx:Application
from http://livedocs.adobe.com/flex/3/html/help.html?content=mxmlcomponents_1.html
Is it possible to use the Flex Framework and Components, without using MXML? I know ActionScript pretty decently, and don't feel like messing around with some new XML language just to get some simple UI in there. Can anyone provide an example consisting of an .as file which can be compiled (ideally via FlashDevelop, though just telling how to do it with the Flex SDK is ok too) and uses the Flex Framework? For example, just showing a Flex button that pops open an Alert would be perfect.
If it's not possible, can someone provide a minimal MXML file which will bootstrap a custom AS class which then has access to the Flex SDK?
I did a simple bootstrap similar to Borek (see below). I would love to get rid of the mxml file, but if I don't have it, I don't get any of the standard themes that come with Flex (haloclassic.swc, etc). Does anybody know how to do what Theo suggests and still have the standard themes applied?
Here's my simplified bootstrapping method:
main.mxml
<?xml version="1.0" encoding="utf-8"?>
<custom:ApplicationClass xmlns:custom="components.*"/>
ApplicationClass.as
package components {
import mx.core.Application;
import mx.events.FlexEvent;
import flash.events.MouseEvent;
import mx.controls.Alert;
import mx.controls.Button;
public class ApplicationClass extends Application {
public function ApplicationClass () {
addEventListener (FlexEvent.CREATION_COMPLETE, handleComplete);
}
private function handleComplete( event : FlexEvent ) : void {
var button : Button = new Button();
button.label = "My favorite button";
button.styleName="halo"
button.addEventListener(MouseEvent.CLICK, handleClick);
addChild( button );
}
private function handleClick(e:MouseEvent):void {
Alert.show("You clicked on the button!", "Clickity");
}
}
}
Here are the necessary updates to use it with Flex 4:
main.mxml
<?xml version="1.0" encoding="utf-8"?>
<local:MyApplication xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:local="components.*" />
MyApplication.as
package components {
import flash.events.MouseEvent;
import mx.controls.Alert;
import mx.events.FlexEvent;
import spark.components.Application;
import spark.components.Button;
public class MyApplication extends Application {
public function MyApplication() {
addEventListener(FlexEvent.CREATION_COMPLETE, creationHandler);
}
private function creationHandler(e:FlexEvent):void {
var button : Button = new Button();
button.label = "My favorite button";
button.styleName="halo"
button.addEventListener(MouseEvent.CLICK, handleClick);
addElement( button );
}
private function handleClick(e:MouseEvent):void {
Alert.show("You clicked it!", "Clickity!");
}
}
}
This is a very simple app that does only the basic bootstrapping in MXML. This is the MXML:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="onCreationComplete()">
<mx:Script source="Script.as" />
</mx:Application>
This is the Script.as:
import mx.controls.Button;
import flash.events.MouseEvent;
import mx.controls.Alert;
import mx.core.Application;
private function onCreationComplete() : void {
var button : Button = new Button();
button.label = "Click me";
button.addEventListener(MouseEvent.CLICK, function(e : MouseEvent) : void {
Alert.show("Clicked");
});
Application.application.addChild(button);
}
NOTE: The below answer will not actually work unless you initialize the Flex library first. There is a lot of code involved to do that. See the comments below, or other answers for more details.
The main class doesn't even have to be in MXML, just create a class that inherits from mx.core.Application (which is what an MXML class with a <mx:Application> root node is compiled as anyway):
package {
import mx.core.Application;
public class MyFancyApplication extends Application {
// do whatever you want here
}
}
Also, any ActionScript code compiled with the mxmlc compiler -- or even the Flash CS3 authoring tool -- can use the Flex classes, it's just a matter of making them available in the classpath (refering to the framework SWC when using mxmlc or pointing to a folder containing the source when using either). Unless the document class inherits from mx.core.Application you might run in to some trouble, though, since some things in the framework assume that this is the case.
Yes, you just need to include the flex swc in your classpath. You can find flex.swc in the flex sdk in frameoworks/lib/flex.swc
edit: One more thing: if you're using Flex Builder you can simply create a new ActionScript project, which will essentially do the same as above.