I have a create characters component with a tabnavigator and textarea. In my main app I want to save the textarea id - mainchar text to the tag. I tried dotnotation, nothing, I tried import components.CreateCharacter; and still nothing, and tried some other options found on the net but can't get this to work.
Note, the code works fine if I call components in the main app, so the code works fine it is just the calling of the component (CreateCharacters) in the components folder. The components folder is in the src folder. Here is the code:
Main.mxml
<fx:Script>
<![CDATA[
import components.CreateCharacters
[Bindable]
public var xmlData:XML=<ROOTS></ROOTS>;
public function sav_clickHandler(event:MouseEvent):void
{
var fr:FileReference = new FileReference();
var ba:ByteArray = new ByteArray();
var newXmlRow:XML=<ROOTS>
<TXT>{components.CreateCharacters.mainchar.text}</TXT>// The problem lies with this line
<TXTA>{txt2.text}</TXTA>
<DTF>{txt3.text}</DTF>
</ROOTS>;
ba.writeMultiByte(newXmlRow, 'utf-8');
fr.save(ba);
}
private var openedFile:File;
private function open_clickHandler(event:MouseEvent):void {
openedFile = new File();
openedFile.addEventListener(Event.SELECT, file_select);
openedFile.browseForOpen("Please select a file...");
}
private function file_select(event:Event):void {
if(openedFile != null && openedFile.exists){
var fileStream:FileStream = new FileStream();
fileStream.open(openedFile, FileMode.READ);
var readXML:XML = XML(fileStream.readUTFBytes(fileStream.bytesAvailable));
fileStream.close();
trace(readXML.toString());
CreateCaracters.maichar.text = readXML.TXT;
txt2.text = readXML.TXTA;
txt3.text = readXML.DTF;
}
trace(event);
}
]]>
</fx:Script>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
<net:FileReference id="fileReference" />
</fx:Declarations>
<s:Image x="0" y="0" width="100%" height="50" scaleMode="stretch"
source="assets/imaginationFly.png"/>
<s:Image x="12" y="1" source="assets/ECWDove.png"/>
<mx:TabNavigator x="1" y="49" width="100%" height="100%" backgroundColor="#7EB7C5"
chromeColor="#85B5BF">
<s:NavigatorContent width="100%" height="100%" label="Start">
<s:TextArea id="txt2" x="57" y="29"/>
</s:NavigatorContent>
<s:NavigatorContent id="Characters" width="100%" height="100%" label="Characters">
<components:CreateCharacters id="creatchr" width="100%" height="100%"/>
</s:NavigatorContent>
<s:NavigatorContent width="100%" height="100%" label="Worlds">
<s:TextArea id="txt3" x="55" y="10"
text="hello
I am testing this shit
hope it works"/>
<s:Button id="sav" x="285" y="138" label="Save" click="sav_clickHandler(event)"/>
<s:Button id="open" x="381" y="138" label="Open" click="open"/>
etc...
CreateCharaters.mxml (component)
<?xml version="1.0" encoding="utf-8"?>
<s:HGroup xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
chromeColor="#0106BD" paddingLeft="10" paddingRight="0">
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<mx:ViewStack id="Characters" x="172" y="10" width="81" height="100%" backgroundColor="#030BB3"
chromeColor="#D7D7D8" paddingLeft="5" paddingRight="15">
<s:NavigatorContent id="Hero" width="100%" height="95%" label="Main Charater">
<s:HGroup width="100%" height="100%">
<s:TextArea id="mainchar" bottom="0" width="80%" height="100%"
chromeColor="#7070FD">//this is the textarea I want to use
<s:text><![CDATA[
Name:
Surname:
Nickname:
Hair Color:
etc...
Any help please
Why would you need binding in that case?
Your XML node is created and the text property is evaluated inside the same function so it should not be a problem. In other words, when the event handler is called, the current value of text will be copied inside your XML node but any future changes won't be reflected (you probably don't need that anyway since you save the XML file when clicking 'Save').
As for the chain in between curly braces, {components.CreateCharacters.mainchar.text} does not seem correct because components.CreateCharacters is a class reference, and not the reference to the component directly. Instead you should use creatchr;
I am not sure if the variable replacement inside XML supports .. Have you tried saving the text value inside a local or a class variable like this ?
var currentText:String = creatchr.mainchar.text;
<TXT>{currentText}</TXT>
Related
I'm trying to calculate a BMI and set a label to the number, but it keeps returning "NaN" rather than the number.
Full code:
<?xml version="1.0" encoding="utf-8"?>
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark" title="BMI Calculator">
<fx:Script>
<![CDATA[
public var weightnum:Number;
public var heightnum:Number;
public var resultvar:Number;
protected function button1_clickHandler():void
{
weightnum = Number(weightvar);
heightnum = Number(heightvar);
resultvar = weightnum * 4.4 / (heightnum * heightnum);
resultstr.text = resultvar.toString();
}
]]>
</fx:Script>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<s:Label left="30" top="30" text="Your Weight (lbs)"/>
<s:TextInput id="weightvar" restrict="0-9" x="30" y="53" width="80%"/>
<s:Label x="30" y="104" text="Your Height (feet)"/>
<s:TextInput id="heightvar" restrict="0-9" x="30" y="127" width="80%"/>
<s:Button x="30" y="202" label="Calculate" click="button1_clickHandler()"/>
<s:Label id="resultstr" x="30" y="253" text="" />
You're trying to cast the TextInput's to a Number (Number(weightvar);).
That obviously won't work.
What you need to do is to cast the TextInput's text property to a Number, just like this:
weightnum = Number(weightvar.text);
Instead of casting using String() use the number.toString()
I recently learned how to work with API's in Flash Builder. So I made this (note: it has no errors and it works in a normal Flex application:
<?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"
width="957" minWidth="800" minHeight="600"
creationComplete="initApp(event)">
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
<s:HTTPService id="myRequest"
resultFormat="e4x"
url="{api_request}"
result="getMyPhotos(event)">
</s:HTTPService>
</fx:Declarations>
<fx:Script>
<![CDATA[
import mx.collections.XMLListCollection;
import mx.events.FlexEvent;
import mx.rpc.events.ResultEvent;
[Bindable]
public var api_request:String;
protected function initApp(event:FlexEvent):void
{
btnClear.addEventListener(MouseEvent.CLICK, leegAlleVelden);
btnSearch.addEventListener(MouseEvent.CLICK, zoekOpFlickr);
flickrFotoCollection.addEventListener(MouseEvent.CLICK, haalFoto);
}
protected function zoekOpFlickr(event:MouseEvent):void
{
var api_URL:String = 'http://api.flickr.com/services/rest/';
var api_method:String = 'flickr.photos.search';
var api_key:String = '290845e3c32e4e10a9e4b09fd993ddb9';
var api_tags:String = txtSearchString.text;
api_request = api_URL + '?method=' + api_method +
'&api_key=' + api_key + '&tags=' + api_tags;
myRequest.send(); // hier maak je connectie met flickr
}
//http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=290845e3c32e4e10a9e4b09fd993ddb9&tags=cat&format=rest&api_sig=93e43cc21b7209d2e59af67736d27eb3
protected function leegAlleVelden(event:MouseEvent):void
{
txtSearchString.text="";
}
protected function getMyPhotos(event:ResultEvent):void
{
//photos allemaal ingelezen
flickrFotoCollection.dataProvider = new XMLListCollection(myRequest.lastResult.photos.photo);
}
public function haalFoto(selectedFoto:Object):String {
// http://farm{farm-id}.static.flickr.com/{server-id}/{id}_{secret}_[mstb].jpg
var farm_id:String = selectedFoto.#farm;
var server_id:String = selectedFoto.#server;
var id:String = selectedFoto.#id;
var secret:String = selectedFoto.#secret;
var size:String = 't';
var api_selectedPhotoURL:String = "http://farm"+farm_id+".static.flickr.com/"+server_id+
"/"+id+"_"+secret+"_"+size+".jpg";
return api_selectedPhotoURL;
}
public function toonFoto(event:MouseEvent):void
{
}
]]>
</fx:Script>
<s:BorderContainer x="0" y="21" width="800" height="100%" backgroundColor="#000000">
<s:HGroup x="30" y="14">
<s:TextInput x="0" y="23" width="267" id="txtSearchString"/>
<s:Button x="289" y="24" label="Search" id="btnSearch" click="zoekOpFlickr(event);"/>
<s:Button x="394" y="24" label="Clear" id="btnClear" click="leegAlleVelden(event);"/>
</s:HGroup>
<s:HGroup x="30" y="54" width="742" height="500">
<mx:Tile width="399" height="500">
<mx:Repeater id="flickrFotoCollection" dataProvider="{myRequest.lastResult.photos.photo}">
<s:Image id="fotoKlein"
source="{haalFoto(flickrFotoCollection.currentItem)}"
click="toonFoto(event);"
width="80" height="80"
scaleMode="stretch"/>
</mx:Repeater>
</mx:Tile>
<s:Image id="fotoGroot" width="336" height="500"/>
</s:HGroup>
</s:BorderContainer>
</s:Application>
Next, I wanted to see if I could get it to work in a 4.5.1 mobile application. I didn't get any errors except for a "could not resolve <mx:Repeater>..." At line 87. So for some reason it doesn't recognize the <mx:Repeater> tag?
I tried to find the cause of this problem, but didn't find it.
Is there a reason why it works in a normal application, but not in a mobile application?
The MX Components are not supported in a Mobile Project. For that code to compile in a Flash Builder mobile project you will have to manually add the SWC with MX components to the library path. Be prepared for poor performance, though.
I have one MXML File as
<objecthandles:ObjectHandles xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" backgroundAlpha="1" xmlns:mx="library://ns.adobe.com/flex/mx"
allowRotate="true" minHeight="25" minWidth="60" height="100" width="200">
<s:BorderContainer id="borderCon" width="100%" height="100%" minHeight="25" minWidth="60"
backgroundAlpha="0" borderVisible="false" borderAlpha="0">
<s:HGroup id="hgText" width="100%" height="100%" gap="0" >
<mx:TextArea id="txtAdd" width="100%" height="100%" color="black"
minHeight="25" minWidth="60" horizontalScrollPolicy="off" verticalScrollPolicy="off" focusOut="txtAddKeyUpHandler(event)"
keyUp="txtAddMirrorKeyUpHandler(event)"
creationComplete="onTextCreationComplete()" />
</s:HGroup>
</s:BorderContainer>
</objecthandles:ObjectHandles>
When ever i create the object of the Fileas
var txtElement:TextElement = new TextElement();
txtElement.txtAdd.text = "Hello";
Then it showing the null object reference that
txtElement.txtAdd seems to be null
Need Perfect Solution?
In the Flex component lifecycle subcomponents will not be created until the parent component is added to the displaylist. Only when the component is added to the displaylist and fully built, will you be able to access its subcomponents. When the component is completely ready for usage, it will dispatch a FlexEvent.CREATION_COMPLETE event.
So do something like this:
var txtElement:TextElement = new TextElement();
txtElement.addEventListener(FlexEvent.CREATION_COMPLETE, initTxtElement);
addElement(txtElement);
private function initTxtElement(event:FlexEvent):void {
txtElement.txtAdd.text = "Hello";
}
Or better yet, since it's a custom component: expose the 'text' property as a property of 'TextElement' and handle the deferred setting of the property internally, so that you can write: txtElement.text = "hello".
I need to call a component named "defectTracker.mxml" by clicking a link in another mxml component called "reviewComponent.mxml". How do I achieve that?
This is my reviewComponent.mxml code:
<?xml version="1.0" encoding="utf-8"?>
<mx:VBox xmlns:mx="http://www.adobe.com/2006/mxml"
width="100%" height="100%"
horizontalScrollPolicy="off" verticalScrollPolicy="off">
<mx:Script>
<![CDATA[
private function defectTrackerLink(event:Event):void{
//call defectTracker
}
]]>
</mx:Script>
<mx:LinkButton label="Delete" textDecoration="underline" textRollOverColor="blue"/>
<mx:LinkButton label="Defect Tracker" textDecoration="underline" textRollOverColor="blue" click="defectTrackerLink(event)"/>
</mx:VBox>
Some one guide me.
Main.mxml:
<mx:Script>
<![CDATA[
private function subBtnBar(evt:ItemClickEvent):void{
switch (evt.label){
case "IQA/UAT":
this.bdyStack.selectedChild = screenIQA;
break;
case "EQA":
Alert.show("Yet To Design");
break;
case "Review Tracker":
this.bdyStack.selectedChild = reviewTracker;
break;
case "Defect Tracker":
this.bdyStack.selectedChild = defectTracker;
break;
default:
trace ("Neither a or b was selected")
}
}
]]>
</mx:Script>
<mx:ViewStack id="tabView" width="910" creationPolicy="all">
<mx:ToggleButtonBar horizontalGap="0" id="subTabBar"
itemClick="subBtnBar(event);" styleName="SubButtonBar"
hideEffect="{dissolveOut}" showEffect="{dissolveIn}">
<mx:dataProvider>
<mx:String>IQA/UAT</mx:String>
<mx:String>EQA</mx:String>
<mx:String>Review Tracker</mx:String>
<mx:String>Defect Tracker</mx:String>
<mx:String>Defect Configuration</mx:String>
<mx:String>Defect Export</mx:String>
<mx:String>Defect Import</mx:String>
</mx:dataProvider>
</mx:ToggleButtonBar>
</mx:ViewStack>
<mx:ViewStack id="bdyStack" width="910" height="80%">
<components:ScrIQA id="screenIQA"
hideEffect="{dissolveOut}" showEffect="{dissolveIn}"/>
<components:scrWorkList id="screenWorkList"
hideEffect="{dissolveOut}" showEffect="{dissolveIn}"/>
<components:DefectEntryVerification id="defectEntryVerification"
hideEffect="{dissolveOut}" showEffect="{dissolveIn}"
width="100%" height="100%"/>
<components:scrDefectResolutionAndCause id="defectResolutionnVerification"
hideEffect="{dissolveOut}" showEffect="{dissolveIn}"
width="100%" height="100%"/>
<components:reviewTracker id="reviewTracker"
hideEffect="{dissolveOut}" showEffect="{dissolveIn}"
width="100%" height="100%"/>
<components:defectTracker id="defectTracker"
hideEffect="{dissolveOut}" showEffect="{dissolveIn}"
width="100%" height="100%"/>
</mx:ViewStack>
The defect Tracker scren is already linked with the main mxml file. How to call the function in the reviewComponent file?
reviewComponent consist of 2 link buttons and it is a column entry of the reviewTracker.mxml file's datagrid. So when I click the link in the review component, I want the defectTracker screen to be called. Its already a child of the main.mxml file.
I tried creting an instance of the main file in the component, and changes the selected child to defect tracker, It shows an error saying:
Error #1009: Cannot access a property or method of a null object reference.
My modified reviewComponent.mxml code:
<?xml version="1.0" encoding="utf-8"?>
<mx:VBox xmlns:mx="http://www.adobe.com/2006/mxml"
width="100%" height="100%"
horizontalScrollPolicy="off" verticalScrollPolicy="off">
<mx:Script>
<![CDATA[
private function defectTrackerLink(event:Event):void{
var main:Main=new Main();
main.bdyStack.selectedChild=main.defectTracker; }
]]>
</mx:Script>
<mx:LinkButton label="Delete" textDecoration="underline" textRollOverColor="blue"/>
<mx:LinkButton label="Defect Tracker" textDecoration="underline" textRollOverColor="blue" click="defectTrackerLink(event)"/>
</mx:VBox>
Please some one guide me in this? Should I call the item click event function of the Toggle button Bar? If so how to do It?
I would use a custom event that bubbles. You would dispatch it from the reviewComponent and it would be caught by the defectTracker.
Here are some good articles that tell you how to create a custom event and how to use it http://livedocs.adobe.com/flex/3/html/help.html?content=createevents_3.html
http://www.connatserdev.com/blog/?p=86
By calling, do you mean adding it to the VBox?
var dTracker:DefectTracker = new DefectTracker();
addChild(dTracker);
Would calling is with a popup work?
var dTracker:DefectTracker = new DefectTracker();
PopUpManager.addPopUp(dTracker, this, true);
I wrote a component that displays a filename, a thumbnail and has a button to load/play the file. The component is databound to a repeater. How can I make it so that the button event fires to the main application and tells it which file to play?
On your custom component you can listen to the button click event and then generate a custom event that holds information about the file you want to play. You can then set the bubbles property to true on the event and dispatch the custom event from your custom component. The bubbles property will make your event float up the display list and reach your main application. Now on your main application you can listen to that event and play the correct file. Hope this helps.
Figured it out (finally)
Custom Component
<?xml version="1.0" encoding="utf-8"?>
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" x="0" y="0" width="215" height="102" styleName="leftListItemPanel" backgroundColor="#ECECEC" horizontalScrollPolicy="off" verticalScrollPolicy="off">
<mx:Script>
<![CDATA[
[Bindable] public var Title:String = "";
[Bindable] public var Description:String = "";
[Bindable] public var Icon:String = "";
[Bindable] public var FileID:String = "";
private function viewClickHandler():void{
dispatchEvent(new Event("viewClick", true));// bubble to parent
}
]]>
</mx:Script>
<mx:Metadata>
[Event(name="viewClick", type="flash.events.Event")]
</mx:Metadata>
<mx:Label x="11" y="9" text="{String(Title)}" styleName="listItemLabel"/>
<mx:TextArea x="11" y="25" height="36" width="170" backgroundAlpha="0.0" alpha="0.0" styleName="listItemDesc" wordWrap="true" editable="false" text="{String(Description)}"/>
<mx:Button x="20" y="65" label="View" click="viewClickHandler();" styleName="listItemButton" height="22" width="60"/>
<mx:LinkButton x="106" y="68" label="Details..." styleName="listItemLink" height="18"/>
<mx:HRule x="0" y="101" width="215"/>
The Repeater
<mx:Canvas id="pnlSpotlight" label="SPOTLIGHT" height="100%" width="100%" horizontalScrollPolicy="off">
<mx:VBox width="100%" height="80%" paddingTop="2" paddingBottom="1" verticalGap="1">
<mx:Repeater id="rptrSpotlight" dataProvider="{aSpotlight}">
<sm:SmallCourseListItem
viewClick="PlayFile(event.currentTarget.getRepeaterItem().fileName);"
Description="{rptrSpotlight.currentItem.fileDescription}"
FileID = "{rptrRecentlyViewed.currentItem.fileName}"
Title="{rptrSpotlight.currentItem.fileTitle}" />
</mx:Repeater>
</mx:VBox>
</mx:Canvas>
Handling function
private function PlayFile(fileName:String):void{
Alert.show(fileName.toString());
}