Flex4: how to create the view states in as class (code-behind) - actionscript-3

According to this document from Adobe Create and apply view states is <s:State/> a state object.
How to create the view states in code-behind ActionScript class?

I have found a simple solution, and I don't have to declare the states using skinning architecture. I don't even to declare the states in my ApplicationClass which extends WindowedApplication.
The solution is: declare the states only in the Main.MXML and by all means with the right namespace, in my case it should be "custom".
Here is the Main.MXML:
<?xml version="1.0" encoding="utf-8"?>
<custom:ApplicationClass xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
xmlns:custom="components.*">
<custom:states>
<s:State name="loginState"/>
<s:State name="infoState"/>
</custom:states>
<s:Panel id="loginPanel" title="Login" includeIn="loginState" />
</custom:ApplicationClass>
I only want to change between different Panels, so this method works well for me. There are certainly other cases, in which the states need to be declared in skinning.
I have got this solution form this link: Error: Could not resolve to a component implementation.

Related

Passing data between MXML files and opening one after Click on another

I am pretty new to flex and was trying to understand how the entire Flex works
Scenario :
I started with creating a single MXML file where there will be a textbox and a button. If the button is clicked a popup will be displayed with the value of the textbox(this worked perfectly). The second thing that came to my mind is how to show the same data in some other page. SO, if i click on the button. It should load another page and display the data that was written in the textbox
My Code till now is this :
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="horizontal">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
public function open(event:MouseEvent):void{
Alert.show("I am opening a page");
Alert.show(txt_inp.text + " is written here");
}
]]>
</mx:Script>
<mx:TextInput id="txt_inp" />
<mx:Button id="openBttn" label="Click Me" click="open(event)" />
</mx:Application>
And Name.xml(The Second page is )
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="setYelp()">
<mx:Script>
<![CDATA[
public var helloYou:HelloYou;
public function yelpHandler():void{
yelp.text = "Ayush"
}
public function setYelp():void {
helloYou.openBttn.addEventListener(MouseEvent.CLICK,yelpHandler);
}
]]>
</mx:Script>
<mx:Label id="yelp" text="yelp">
</mx:Label>
</mx:Application>
I referred to some of the previously asked questions like this and tried following it, but couldn't really understand.
Also, in my flex builder I had two more options of MXML > MXML Module and MXML Component. Should I use them here, if not, when should I use it?
Why are you using multiple page? usually a Flex application is a single application with multiple views and sharing data is way easier in this situation.
If you want to pass information between different pages you can use cookies (see for example Accessing browser cookies from Flex)
Another flex solution is this http://custardbelly.com/blog/blog-posts/2010/11/15/flex-hero-persistant-data-in-mobileapplication/
The last thing that I can think about is using the javascript bridge to access the localstorage (o sqlite) of your browser and store information there.
But again why save information between requests and not simply create a single application that changes its views ?
1st solution
Use ViewStack component to integrate your 1st mxml and 2nd mxml.
This is what #wezzy said "single application with multiple views".
This is the most easily way to share values, if you can integrate to one mxml.
like this
<mx:ViewStack id="viewstack" width="100%" height="100%" selectedIndex="0" >
<!-- Input Page is here -->
<mx:Canvas id="view1" width="100%" height="100%">
<mx:TextInput id="txtName" width="150" maxChars="40" />
<mx:Button label="Go to NextPage" click="viewstack.selectedChild=view2;"/>
</mx:Canvas>
<!-- Result Page -->
<mx:Canvas id="view2" width="100%" height="100%">
<mx:TextInput text="{txtName.text}" width="150" editable="false" />
<mx:Button label="Back to PrevPage" click="viewstack.selectedChild=view1;"/>
</mx:Canvas>
</mx:ViewStack>
2nd solution
Use SharedObject(or DB) to save and load values.
3rd solution
Use singleton variables to share the values between classes.
Please see this answer of mine.
Flex - Problems in accessing static variable on another mxml page

What does alpha.disabled mean in Spark's ButtonSkin in AS3?

Take the following code from spark.skins.spark.ButtonSkin.mxml:
<s:SparkButtonSkin xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:fb="http://ns.adobe.com/flashbuilder/2009"
minWidth="21" minHeight="21"
alpha.disabled="0.5">
What does alpha.disabled mean? Because alpha is a Number-type getter in class mx.core.UIComponent, and Numbers don't have a property called disabled. Even if they did, I've never seen a property of a getter being set to something in an MXML element tag before, at least that I can remember.
I've tried to look in a couple of other spots to find what's going on there, but what does setting alpha.disabled like this mean? What construct of the language is being used here? Thanks.
This is a reference to the "new" state syntax introduced in Flex 4.0. It means that when the state of the component is set to disabled the alpha property will be 0.5 . Otherwise the alpha state will be the default value--which I I assume 1.
When creating my own components; and using this approach, I like to specify values for every state. Sometimes I use the 'catch' all. Like this:
<s:SparkButtonSkin xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:fb="http://ns.adobe.com/flashbuilder/2009"
minWidth="21" minHeight="21"
alpha="0.7"
alpha.normal="1"
alpha.disabled="0.5"
>
The above code means that in the disabled state the alpha property will be 0.5. In the normal state the alpha will be 1. In all other states the alpha will be 0.7.
The 'state' name after the property can also refer to a state group instead of an explicit state.

Enable Button only when 2 TextInput fields are not empty - code and screenshot attached

In a Flex mobile app (or any Flex 4 application) - how can you enable/disable a button depending on the contents of 2 other fields?
I am pasting my very simple test code below and the problem with it is the syntax error in Flash Builder 4.7: The entity name must immediately follow the '&' in the entity reference. - which probably means that ampersand is a special character, but how to solve this (probably frequent) problem?
TestApp.mxml:
<?xml version="1.0" encoding="utf-8"?>
<s:ViewNavigatorApplication
xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
firstView="views.Home"
applicationDPI="160">
</s:ViewNavigatorApplication>
views/Home.mxml:
<?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="How to enable Login button?">
<s:layout>
<s:VerticalLayout paddingTop="8"
horizontalAlign="center" gap="8" />
</s:layout>
<s:Label text="Username:" />
<s:TextInput id="_username" />
<s:Label text="Password:" />
<s:TextInput id="_password" />
<s:Button id="_login"
label="Login"
enabled="{_username.text.length > 0 && _password.text.length > 0}" />
</s:View>
You shall replace && with && when you write it outside of CDATA in mxml.
It is also better to use _username.text!='' instead of _username.text.length > 0 because it will cause warnings at runtime as text is not an eventdispatcher and it can't report length changes. However, it will update button's availablity because text will be changed itself and TextInput will report it causing binding update.
when using binding expressions. mxml does not consider && rather write your expression as
& amp ; & amp ;
....also mxml does not read escape characters e.g. \t for tab...rather write your expression in actionscript then use data binding .cheers ;-)

What is the hostComponent?

Im skinning a progressBar in Flex, and after reading a bit about it, I see that there is something called hostComponent.
Adobe site says:
"The host component is the component that uses the skin. By specifying the host component, Spark skins can gain a reference to the component instance that uses the skin by using the hostComponent property."
But, I still dont understand how this exactly works.
Any quick and practical explanation?
Thanks!
When you create custom components in the Spark architecture, you usually split them up into two parts:
an ActionScript class that contains the core functionality of the custom component. This class will usually extend SkinnableComponent or SkinnableContainer
an MXML skin class which is loosely associated with that ActionScript class and contains only the visual presentation of the component. This class should contain no real functionality and it should be trivial to substitute it with another skin.
The first of these two classes is referred to as the host component from the skin's point of view.
A simple example
Let's create a very simple panel by extending SkinnableContainer:
public class MyPanel extends SkinnableContainer {
[Bindable]
public var title:String;
}
As you can see, I made a property 'title' which we want to use to display a title in the Panel. Now let's create a skin that uses this property:
<?xml version="1.0" encoding="utf-8"?>
<s:Skin xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark">
<fx:Metadata>
[HostComponent("path.to.MyPanel")]
</fx:Metadata>
<!-- graphics for title bar go here -->
<s:Label text="{hostComponent.title}" top="5" left="5" />
<!-- graphics for panel content go here -->
<s:Group id="contentGroup" top="30" bottom="0" left="0" right="0" />
</s:Skin>
The hostcomponent is defined in the 'metadata' block and you see that we can use it to bind its properties into our visual representation. The 'contentGroup' is there because it is required by SkinnableContainer; this is were all the elements will go that you put inside the custom panel. So here's how to use it:
<myComps:MyPanel title="Panel title" skinClass="path.to.skins.MyPanelSkin">
<s:Label text="Hello Panel" />
<!--everything in here goes into the 'contentGroup'-->
</myComps:MyPanel>

AmChart component is not rendering (AIR app)

Our AIR app consists from multiple modules. Each module extends MovieClip class. But when I am adding AmChart component to one of these modules, it is not rendering. Here the sample (PieChart.mxml):
<?xml version="1.0" encoding="utf-8"?>
<s:SkinnableContainer xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
xmlns:amcharts="http://www.amcharts.com/com_internal"
width="100%" height="100%">
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
[Bindable]
private var chartData:ArrayCollection = new ArrayCollection([
{country:"Czech Republic",litres:156.90},
{country:"Ireland",litres:131.10},
{country:"Germany",litres:115.80},
{country:"Australia",litres:109.90},
{country:"Austria",litres:108.30},
{country:"UK",litres:99.00},
{country:"Belgium",litres:93.00}]);
]]>
</fx:Script>
<amcharts:AmPieChart width="100%" height="100%" dataProvider="{chartData}" titleField="country" valueField="litres"/>
</s:SkinnableContainer>
And this is a sample usage:
var chart:PieChart = new PieChart();
cContainer.addChild(chart);
cContainer - is instance of MovieClip.
Can you suggest why this is not rendering? Maybe this seems a silly question, but I am a quite newbie to Flex. Please, help.
You may want to try using the dataset syntax used here http://blog.amcharts.com/2010/11/amstock-flex-tutorial-part-1-basics.html, and also try using a number for width and height until you get it working, rather than a percentage. If neither of those works, you may want to try asking the people who wrote this.
HTH;
Amy