Setting bordercontainer backgroundimage from inside component - apache-flex

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!

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

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

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>

how to print a value in a next mxml page where the given input is from prev page?

<?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="1501" height="960" minWidth="955" minHeight="600">
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Script>
<![CDATA[
protected function c_clickHandler(event:MouseEvent):void
{
viewstack1.selectedIndex = 2;
}
]]>
</fx:Script>
<fx:DesignLayer>
</fx:DesignLayer>
<mx:ViewStack id="viewstack1" x="357" y="256" width="624" height="200">
<s:NavigatorContent width="100%" height="100%" label="configuration"
backgroundColor="#F7C2C2">
<s:Button x="261" y="72" id="c" click="c_clickHandler(event)" label="new config"/>
<s:TextInput x="240" y="25"/>
</s:NavigatorContent>
<s:NavigatorContent width="100%" height="100%" label="system" backgroundColor="#F45F5F">
<s:TextArea x="218" y="39" height="35"/>
</s:NavigatorContent>
</mx:ViewStack>
<s:ButtonBar x="531" y="224" dataProvider="{viewstack1}"/>
</s:Application>
i have a text inputbox in a config tab. when a user gives a input and click the "set new config" button it will go to a syatem where there will be a textbox.In thet textbox the prev input will be shown.
how to do this?
can u provide me some codes?
Thanks
use binding
Text input in your config tab
`<s:TextInput x="240" y="25" id="_txt"/>
and set text in another tab change to
<s:TextArea text="{_txt.text}"/>
`

Set dataprovider of buttonbar component

I am trying to add a navigation component with a button bar in it that controls the view stack of the main application. Here is what I have so far for the main application's 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"
xmlns:comps="comps.*" minWidth="955" minHeight="600">
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Script>
<![CDATA[
[Bindable]
public var myViewStack:ViewStack;;
]]>
</fx:Script>
<fx:Binding source="lgViewStack" destination="myViewStack"/>
<comps:viewControl id="myControl" width="935" horizontalCenter="0" top="5" height="134"/>
<mx:ViewStack id="lgViewStack" width="935" height="474" left="10" verticalCenter="66">
<s:NavigatorContent label="View 1" width="100%" height="100%" id="view1">
<s:Panel id="firstPanel" title="First Panel" width="250" height="200" horizontalCenter="0" verticalCenter="0" >
</s:Panel>
</s:NavigatorContent>
<s:NavigatorContent label="View 2" width="100%" height="100%" id="view2">
<s:Panel id="secondView" title="Second View" width="250" height="200" horizontalCenter="0" verticalCenter="0" >
</s:Panel>
</s:NavigatorContent>
</mx:ViewStack>
</s:Application>
And the component code:
<?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="400" height="300">
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<s:ButtonBar x="170" y="10" width="58" dataProvider="{myViewStack}"/>
</s:Group>
I receive a compile error from the component: Access of undefined property myViewStack. Am I referencing the variable myViewStack incorrectly? I am still trying to understand how bindings work.
ViewControl
<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">
<fx:Script>
<![CDATA[
public function set dataProvider(value:ViewStack):void
{
bar.dataProvider = value;
}
public function get dataProvider():ViewStack
{
return bar.dataProvider;
}
]]>
</fx:Script>
<s:ButtonBar id="bar"/>
</s:Group>
Main
<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"
xmlns:comps="comps.*">
<comps:ViewControl dataProvider="{viewstack}"/>
<mx:ViewStack id="viewstack" />
</s:Application>
Should also be noted that you could always just extend ButtonBar in your custom component instead depending what you're trying to accomplish by creating that component.
You should move public var myViewstack:ViewStack field declaration into your component. Think about MXML components as ordinary classes according with OOP conceptions. So for your 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"
xmlns:comps="comps.*" minWidth="955" minHeight="600">
<comps:viewControl id="myControl" width="935" horizontalCenter="0" top="5" height="134" myViewstack="{lgViewStack}" />
<mx:ViewStack id="lgViewStack" width="935" height="474" left="10" verticalCenter="66">
<s:NavigatorContent label="View 1" width="100%" height="100%" id="view1">
<s:Panel id="firstPanel" title="First Panel" width="250" height="200" horizontalCenter="0" verticalCenter="0" >
<s:Label id="testSource" text="Text to copy" />
</s:Panel>
</s:NavigatorContent>
<s:NavigatorContent label="View 2" width="100%" height="100%" id="view2">
<s:Panel id="secondView" title="Second View" width="250" height="200" horizontalCenter="0" verticalCenter="0" >
</s:Panel>
</s:NavigatorContent>
</mx:ViewStack>
</s:Application>
and
<?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="400" height="300">
<fx:Script>
<![CDATA[
[Bindable]
public var myViewstack:ViewStack;
]]>
</fx:Script>
<s:ButtonBar x="170" y="10" width="58" dataProvider="{myViewStack}"/>
</s:Group>

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

Resources