Páginas

14 Abril 2010

Mudar estado do ViewStack com um MenuBar

Este é um exemplo de como selecionar uma view de um ViewStack a partir de um item de Menubar.

Normalmente ligamos o dataProvider de um ViewStack com algum componente como o LinkBar. No caso do MenuBar o que fazemos é selecionar uma view a partir de um valor definido no XML usando como dataProvider do MenuBar.

Tuto.mxml:

<?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">
<s:layout>
<s:VerticalLayout/>
</s:layout>
<fx:Script source="Tuto.as" />
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
<fx:XML id="xmlTuto" xmlns="">
<menubar>
<item label="Item 1">
<item label="Item 1-1" view="view1" />
<item label="Item 1-2" view="view2" />
</item>
</menubar>
</fx:XML>
</fx:Declarations>
<mx:MenuBar id="menuTuto"
dataProvider="{xmlTuto}"
labelField="@label"
showRoot="false"
width="100%" itemClick="menuTuto_itemClickHandler(event)">
</mx:MenuBar>
<mx:ViewStack id="viewTuto">
<s:NavigatorContent id="view1" width="100%" height="100%">
<s:Label text="view1" />
</s:NavigatorContent>
<s:NavigatorContent id="view2" width="100%" height="100%">
<s:Label text="view2" />
</s:NavigatorContent>
</mx:ViewStack>
</s:Application>



Tuto.as:




// ActionScript file
import mx.core.INavigatorContent;
import mx.events.MenuEvent;

protected function menuTuto_itemClickHandler(event:MenuEvent):void
{
if (event.item.@view != null)
viewTuto.selectedChild = INavigatorContent(
viewTuto.getChildByName( event.item.@view));
}



Para melhorar a visualização separei em dois arquivos. Tuto.as é incluido em Tuto.mxml através do comando fx:Script.



Download do Projeto