Is it possible to use outside "<mx:script>" in "<mx:canvas>"? - apache-flex

I got a new project. and some part of it, Flex was exist.
<mx: Application xmlns:mx=...>
<mx:script>
import...
function A() {
}
</mx:script>
<mx:linkBar...>
<mxViewStack ...>
<mx:Canvas id="1st" ...> **[HERE]** </mx:Canvas>
<mx:canvas id="2nd" ...> ... </mx:Canvas>
<mx:canvas id="3rd" ...> ... </mx:Canvas>
** When Viewstack calls Canvas sequentially, A() has to work.
Is it possible to use function A() in **HERE**]?
Or please let me know the possible function or tag which can be used.

Below example may help you: -
<?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" minWidth="955" minHeight="600">
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Script>
<![CDATA[
private function A():void
{
//Your code
}
private function viewStackHandler():void
{
//Method One
if(viewStackID.selectedIndex == 0)
{
A();
}
else if(viewStackID.selectedIndex == 1)
{
A();
}
else
{
A();
}
//OR Method Second
//call only A()
}
]]>
</fx:Script>
<mx:VBox width="100%" height="100%">
<mx:HBox width="100%" height="20">
<s:Button label="One" click="{viewStackID.selectedIndex = 0}"/>
<s:Button label="Two" click="{viewStackID.selectedIndex = 1}"/>
<s:Button label="Three" click="{viewStackID.selectedIndex = 2}"/>
</mx:HBox>
<mx:ViewStack width="500" height="400" id="viewStackID" change="viewStackHandler()">
<mx:Canvas id="canval1" borderColor="#FF0000" width="100%" height="100%">
<s:Button label="One"/>
</mx:Canvas>
<mx:Canvas id="canval2" borderColor="#00FF00" width="100%" height="100%">
<s:Button label="Two"/>
</mx:Canvas>
<mx:Canvas id="canval3" borderColor="#0000FF" width="100%" height="100%">
<s:Button label="Three"/>
</mx:Canvas>
</mx:ViewStack>
</mx:VBox>
</s:Application>

You can use the add attribute which is dispatched when the Canvas is added to the ViewStack
<mx:Canvas id="1st" add="A()"></mx:Canvas>

Related

Display data properties of ItemRenderer

I have this ItemRenderer
<?xml version="1.0" encoding="utf-8"?>
<s:ItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
autoDrawBackground="false">
<s:HGroup verticalAlign="middle">
<s:Button label="{data.Nome} ({data.Rating})" width="150" height="35"/>
<s:Button label="{data.Estado}" width="150" height="30"/>
</s:HGroup>
</s:ItemRenderer>
I'd like to see the properties of data object when typing . since its a custom object. How can I see them?
data is suppose to be a User class object.
Try this
<fx:Script>
<![CDATA[
import yourPackage.User;
[Bindable]
private var user:User;
override public function set data(value:Object):void{
super.data = value;
user = data as User;
}
]]>
</fx:Script>
<s:HGroup verticalAlign="middle">
<s:Button label="{user.Nome} ({user.Rating})" width="150" height="35"/>
<s:Button label="{user.Estado}" width="150" height="30"/>
</s:HGroup>
Either as Юрий Борыс said or you could also cast data as User:
<s:Button label="{User(data).Nome} ({User(data).Rating})" width="150" height="35"/>
HIH

How to draw not out of panel borders in Flex?

I want to draw some lines, they must not goes across the panel borders, I don't know how to do that...
Line goes over the panel borders, but I want that line goes into panel and then i would can zoom it.
Here is my code:
<?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" minWidth="955" minHeight="600"
>
<fx:Script>
<![CDATA[
import mx.graphics.SolidColorStroke;
import spark.primitives.Line;
private function drawLine():void
{
var myShape:Shape = new Shape();
myShape = new Shape() ;
myShape.graphics.lineStyle(2, 0x00008B, .75);
myShape.graphics.moveTo(500, 500);
myShape.graphics.lineTo(25, 45);
spr.addChild(myShape);
}
]]>
</fx:Script>
<mx:Button label="Click" click="drawLine()" />
<s:Panel y="30" width="400" height="250" layout="vertical" title="Panel" borderColor="#008040" fontFamily="Arial" fontWeight="bold" fontSize="13">
<mx:Text text="Drawing.." fontWeight="bold"/>
<s:SpriteVisualElement width="500" height="500" id="spr"/>
</s:Panel>
</s:Application>
One simple way is to put the contents in a Group and set clipAndEnableScrolling.
<s:Panel>
<s:VGroup height="100%" width="100%" clipAndEnableScrolling="true">
<mx:Text text="Drawing.." fontWeight="bold"/>
<s:SpriteVisualElement width="500" height="500" id="spr"/>
</s:VGroup>
</s:Panel>

Setting bordercontainer backgroundimage from inside component

For my flex project I am making, I'm trying to change the background of my website change to the image I have clicked on. The background in my main page I have set like this:
<s:BorderContainer id="backgroundContainer" width="100%" height="100%" backgroundImage="#Embed('assets/background.png')" borderAlpha="0">
<s:layout>
<s:VerticalLayout horizontalAlign="center"/>
</s:layout>
<mx:LinkBar styleName="mainnav" width="600" dataProvider="content" horizontalCenter="0" paddingLeft="20" paddingTop="125"/>
<s:Image top="5" bottom="5" horizontalCenter="50" source="assets/nav.png"/>
<mx:ViewStack id="content">
<mx:HBox id="home"
label="Home">
<component:home/>
</mx:HBox>
<mx:HBox id="bio"
label="Bio">
<component:bio/>
</mx:HBox>
<mx:HBox id="portfolio"
label="Portfolio">
<component:portfolio/>
</mx:HBox>
<mx:HBox id="contact"
label="Contact">
<component:contact/>
</mx:HBox>
</mx:ViewStack>
</s:BorderContainer>
Now from inside my component, I am trying to set the the background of the image you click on.
<fx:Script>
<![CDATA[
import mx.core.Application;
public function changeBackground(event:MouseEvent):void
{
Application.application.backgroundContainer.setStyle('backgroundImage', img1.source);
}
]]>
</fx:Script>
I call this function when you click on an image.
<mx:Image id="img1" source="assets/placeholder.jpg" click="changeBackground(event)"/>
But it doesn't work.
So I was wondering how this should be done?
Thank you,
Thomas
you can use BitmapFill and declare all your backgrounds first. Also, be sure your changeBackground function is public in your main application since it will be called from a component.
<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication 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:component="component.*">
<fx:Script>
<![CDATA[
public function changeBackground(bitmapFillObj:BitmapFill):void
{
backgroundContainer.backgroundFill = bitmapFillObj;
}
]]>
</fx:Script>
<fx:Declarations>
<s:BitmapFill id="_bg1" source="#Embed('assets/bg1.jpg')"/>
<s:BitmapFill id="_bg2" source="#Embed('assets/bg2.jpg')"/>
</fx:Declarations>
<s:BorderContainer id="backgroundContainer" width="100%" height="100%" backgroundImage="#Embed('assets/bg1.jpg')" borderAlpha="0">
<component:home/>
</s:BorderContainer>
</s:WindowedApplication>
And now your component
<fx:Script>
<![CDATA[
import mx.core.FlexGlobals;
]]>
</fx:Script>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<s:layout>
<s:HorizontalLayout/>
</s:layout>
<s:Button label="click to show bg 1" click="FlexGlobals.topLevelApplication.changeBackground(FlexGlobals.topLevelApplication._bg1)"/>
<s:Button label="click to show bg 2" click="FlexGlobals.topLevelApplication.changeBackground(FlexGlobals.topLevelApplication._bg2)"/>
Your just have to adapt the code to your project, clicking on your images instead of buttons. Good luck!

Component goes out of another component

If I drag a textInput from a component and drop it near the end of the another component, the textInput goes outside of the dropZone.
Any ideas how to resolve this problem?
thanks
edit:
<?xml version="1.0" encoding="utf-8"?>
<s:Group 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="150" height="500" xmlns:components="components.*" >
protected function textInput_mouseDownHandler(event:MouseEvent):void
{
var dragI:Label=event.currentTarget as Label;
var ds:DragSource=new DragSource();
ds.addData(dragI,"TextInput");
DragManager.doDrag(dragI,ds,event);
}
<s:BorderContainer width="100%" height="100%">
<s:layout>
<s:VerticalLayout gap="10"/>
</s:layout>
<s:Label text="TextInput" mouseMove="textInput_mouseDownHandler(event )" dragComplete="dragCompleteHandler(event)" />
</s:BorderContainer>
and the dropZone componenent:
<?xml version="1.0" encoding="utf-8"?>
<s:Group xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" height="500" width="700" xmlns:components="components.*" >
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
protected function dragDropHandler(event:DragEvent):void
{
if (event.dragSource.hasFormat("TextInput")){
var draggedText:TextInput=new TextInput();
draggedText.x = event.currentTarget.mouseX;
draggedText.y = event.currentTarget.mouseY;
event.currentTarget.addElement(draggedText);
}
}
protected function dragEnterHandler(event:DragEvent):void
{
if(event.dragSource.hasFormat("TextInput") )
{
var dropTarget:BorderContainer= event.currentTarget as BorderContainer;
DragManager.acceptDragDrop(dropTarget);
}
}
<s:BorderContainer width="100%" height="100%" dragDrop="dragDropHandler(event)"
dragEnter="dragEnterHandler(event)"/>
</s:Group>
On your layout object, set clipAndEnableScrolling to true. This will cause the layout to clip all content that is ouside the bounds of the containter. If you need to scroll, then you still need to wrap your group in a scroller.
<s:BorderContainer width="100%" height="100%">
<s:layout>
<s:VerticalLayout gap="10" clipAndEnableScrolling="true"/>
</s:layout>
<s:Label text="TextInput" mouseMove="textInput_mouseDownHandler(event )" dragComplete="dragCompleteHandler(event)" />
</s:BorderContainer>
clipAndEnableScrolling is a property on all the layouts, VerticalLayout, HorizontalLayout, and BasicLayout to name a few.
http://flexonblog.wordpress.com/2009/03/02/drag-and-drop-from-list-control-to-textinputnon-list-control-control-in-flex/
http://saturnboy.com/2009/08/drag-and-drop-flex-4/
http://help.adobe.com/en_US/flex/using/WS2db454920e96a9e51e63e3d11c0bf64595-7fff.html

Flex Question: Can I use a ComboBox in Flex to change a view stack?

I am trying to use a combobox in flex with an array to change to a canvas in a view stack.
Can I do this with some custom action script? The UI I am designing could really benefit form this.
Here's a little demo app that demonstrates what you are trying to do:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical">
<mx:Script>
<![CDATA[
import mx.core.Container;
private function onComboBoxChange():void
{
stackNav.selectedChild = this[comboNav.selectedItem];
}
]]>
</mx:Script>
<mx:ComboBox id="comboNav" dataProvider="{['canvas1','canvas2']}" change="onComboBoxChange()"/>
<mx:ViewStack id="stackNav" width="400" height="300">
<mx:Canvas id="canvas1" width="100%" height="100%">
<mx:Label text="Hello" horizontalCenter="0" verticalCenter="0"/>
</mx:Canvas>
<mx:Canvas id="canvas2" width="100%" height="100%">
<mx:Label text="World!" horizontalCenter="0" verticalCenter="0"/>
</mx:Canvas>
</mx:ViewStack>
</mx:Application>
You can change the logic in onComboBoxChange() to use selectedIndex as well:
stackNav.selectedIndex = comboNav.selectedIndex;
Or you can bind the selectedIndex of the viewStack to the selectedIndex property of the combo:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical">
<mx:ComboBox id="comboNav" dataProvider="{['canvas1','canvas2']}"/>
<mx:ViewStack id="stackNav" width="400" height="300"
selectedIndex="{comboNav.selectedIndex}">
<mx:Canvas id="canvas1" width="100%" height="100%">
<mx:Label text="Hello" horizontalCenter="0" verticalCenter="0"/>
</mx:Canvas>
<mx:Canvas id="canvas2" width="100%" height="100%">
<mx:Label text="World!" horizontalCenter="0" verticalCenter="0"/>
</mx:Canvas>
</mx:ViewStack>
</mx:Application>
You can use this solution, but be warned that you can not pike a word with space
(try to change "canvas1" to "canvas 1") and you will see:
[Bindable] private var models:Array = ["tasks","users","bugs"];
public function changeViewStackModel():void {
//this.modelViewStack.selectedChild=users; //works
//this.modelViewStack.selectedChild="users"; //does not work
//this.modelViewStack.selectedChild=this.modelsCombo.selectedItem; //does not work
switch(modelsCombo.selectedItem) {
case "tasks": modelViewStack.selectedChild=tasks; break;
case "users": modelViewStack.selectedChild=users; break;
case "bugs": modelViewStack.selectedChild=bugs; break;
}
}
MXML code:
<mx:ComboBox id="modelsCombo" dataProvider="{models}" selectedIndex="0"
change="changeViewStackModel()"/>
<mx:ViewStack x="29.25" y="55" id="modelViewStack" width="90%" height="200">
<mx:Canvas id="tasks" label="Tasks"><mx:Label text="Tasks stack view!!"/>
</mx:Canvas>
<mx:Canvas id="users" label="Users"><mx:Label text="Users stack view!!"/>
</mx:Canvas>
<mx:Canvas id="bugs" label="Bugs"><mx:Label text="Bugs stack view!!"/>
</mx:Canvas>
</mx:ViewStack>

Resources