首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 媒体动画 > flex >

怎么在Flex的自定义组件中调用程序中的ViewStack,来进行页面切换

2012-07-23 
如何在Flex的自定义组件中调用程序中的ViewStack,来进行页面切换。我在Flex项目中自定义了一个组件,把它加

如何在Flex的自定义组件中调用程序中的ViewStack,来进行页面切换。
我在Flex项目中自定义了一个组件,把它加入到程序中的ViewStack中去,这个组件上有一个按钮,想用它来实现页面的切换。但是不能直接调用。希望哪位高手能给出解决办法。我也想到了建一个as文件来维护一个值。用这个值来控制ViewStack的显示页面。但是没有实现成功。希望能得到详细一些的指导。谢谢!

[解决办法]
楼主请看我的留言板项目源码,相信对你有所帮助,里面就用到了自定义组件的viewstack:http://download.csdn.net/source/643705
[解决办法]
<?xml version="1.0" encoding="utf-8"?>
<mx:Panel xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" xmlns:view="view.*" styleName="myPanel" creationComplete="init()" title="查看留言列表" width="480" height="334">
<mx:Script>
<![CDATA[
import events.replyNoteEvent;
import vo.notevo;
import mx.controls.Alert;
import events.noteEvent;
import control.noteControl;
import model.ModelLocator;
[Bindable]
public var ins:ModelLocator=ModelLocator.getInstance();
private var dbcon:noteControl=new noteControl();
internal function init():void{
ins.addEventListener(noteEvent.addNoteEvent,addviewhandler);
ins.addEventListener(noteEvent.replyNoteResult,eventhandler);
ins.addEventListener(noteEvent.replyNoteFail,eventhandler);
ins.addEventListener(noteEvent.removeNoteResult,eventhandler);
ins.addEventListener(noteEvent.removeNoteFail,eventhandler);
}
internal function loadData():void{
dbcon.getAllNotes();
}
internal function addviewhandler(evt:Event):void{

notevs.selectedChild=add_note;
}
internal function eventhandler(evt:Event):void{
if(evt.type == noteEvent.replyNoteResult){
Alert.show("回复已成功保存","提示");
}else if(evt.type == noteEvent.replyNoteFail){
Alert.show("添加回复失败,请重新再试!!","提示");
}else if(evt.type == noteEvent.removeNoteResult){
Alert.show("记录已被删除!!","提示");
notevs.selectedChild=list_note;
}else if(evt.type == noteEvent.removeNoteFail){
Alert.show("无法删除记录,请重新再试!!","提示");
}
}
internal function selectNote():void{
var note:notevo=notelist.selectedItem as notevo;
ins.selectedNote=note;
notevs.selectedChild=view_note;
}
internal function viewnotelist():void{
notevs.selectedChild=list_note;
}
internal function changeview():void{
btnref.visible=tiptxt.visible=false;
if(notevs.selectedChild == list_note){
btnref.visible=tiptxt.visible=true;
title="查看留言列表";
}else if(notevs.selectedChild == view_note){
title="查看留言详情";
}else{
title="撰写新留言";
}
}
internal function saveReply(evt:replyNoteEvent):void{
dbcon.replyNote(evt.nid,evt.reply);
}
internal function removeNote(evt:Event):void{
dbcon.removeNote(ins.selectedNote.nid);
}
]]>
</mx:Script>
<mx:ViewStack x="10" y="10" id="notevs" width="480" height="400" change="changeview()">
<mx:Canvas id="list_note" width="100%" height="100%" showEffect="WipeDown" hideEffect="WipeUp">
<mx:List id="notelist" itemClick="selectNote()" styleName="myList" itemRenderer="view.noteThumb" dataProvider="{ins.notes}" width="100%" height="100%"/>
</mx:Canvas>
<view:noteDetail id="view_note" showEffect="WipeDown" hideEffect="WipeUp" backtolist="viewnotelist()" savereply="saveReply(event)" removeNote="removeNote(event)"/>


<view:addNote id="add_note" showEffect="WipeDown" hideEffect="WipeUp" backtolist="viewnotelist()"/>
</mx:ViewStack>
<mx:ControlBar height="27" y="277">
<mx:Label text="当前一共有{ins.notes.length}条留言" id="tiptxt" width="269" height="20"/>
<mx:Spacer width="100%"/>
<mx:LinkButton label="刷新数据" width="72" id="btnref" click="loadData()" />
</mx:ControlBar>
</mx:Panel>

[解决办法]
selectedIndex:int [] 


当前可见子容器的从零开始的索引。子索引的范围是 0、1、2、...、n - 1,其中 n 是子项的数目。默认值是 0,对应于第一个子项。如果不存在子容器,则此属性的值为 -1。 

注意:当您将一个新的子容器添加到 ViewStack 容器时,selectedIndex 属性将根据需要自动调整,以使所选子容器仍处于选中状态。


此属性可用作数据绑定的源。


[解决办法]
刚接触Flex,还看不太明白。我就是想知道在自定义控件里,如何改变外层ViewStack的页面顺序。因为自定义控件在ViewStack中所以无法直接用mystack.SelectChild=XX这样的方法来调用。

Re: 
not always like that. dosent matter SelectChild or SlectedIndex you are using.

remember, if you are in a component, and you want to reference your main application or parent application. There is a very general method to call it,

Assume in your <mx:comp >

this.parent.Application.*
<!--
 this is the component,
 this.parent, is referencing its parent,
 this.parent.Application.* can invite/invoke any property/method in the parent.
 this is because, the parent variable names is all public to its child(if not specify),
 and the child's property and method can be invoked by the compId.*, which is property/method name in child
-->
</mx:comp>
[解决办法]
在自定义容器里this.parent.mystack.selectedIndex = X;
或者Application.application.mystack.selectedIndex = X;都可以吧。

热点排行