我试一试,碰巧我很难弄清楚如何在TabbedMobileApplication中的ViewNavigator之间传递数据。
<s:TabbedMobileApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark">
<fx:Declarations>
</fx:Declarations>
<s:ViewNavigator id="nav1" label="Nav1" firstView="views.Nav1Home" width="100%" height="100%"/>
<s:ViewNavigator id="nav2" label="Nav2" firstView="views.Nav2Home" width="100%" height="100%"/>
</s:TabbedMobileApplication>
如何在nav1和nav2之间传递数据?我知道如何在导航视图之间进行操作。
谢谢, 乙
答案 0 :(得分:2)
我想补充一点,我更喜欢比已经讨论过的更松散耦合的通信机制。我更喜欢某种类型的事件聚合系统,其中解耦组件发送消息而彼此不知道另一个。
这种类型的事件系统内置于轻量级框架中,例如Parsely和Robot Legs。
这主要是一种风格的东西,但根据我的经验,我们越是紧密地联系这样的沟通,我们就越需要付出代价。
我认为,值得深思。 :)答案 1 :(得分:2)
一种简单的方法是使用ViewNavigator将数据对象添加到视图中。然后在子视图中,使用它来获取数据:
this.parentDocument.yourDataObject
答案 2 :(得分:1)
除非我在这里遗漏了什么,否则你要做的就是在这里找一个脚本块并听取你的ViewNavigators的事件。然后在事件的处理程序中,调用目标ViewNav上的公共函数。
<s:TabbedMobileApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark" creationComplete="init()">
<fx:Declarations>
</fx:Declarations>
<fx:script><![CDATA[
private function init():void{
nav1.addEvenListener(CustomEvent.DATA, onData);
}
private function onData(ev:CustomEvent):void{
nav2.setData(ev.data);
}
]]></fx:script>
<s:ViewNavigator id="nav1" label="Nav1" firstView="views.Nav1Home" width="100%" height="100%"/>
<s:ViewNavigator id="nav2" label="Nav2" firstView="views.Nav2Home" width="100%" height="100%"/>
</s:TabbedMobileApplication>
答案 3 :(得分:1)
<强> TestApplication.mxml 强>
<s:TabbedMobileApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
creationComplete="init()">
<fx:Declarations>
</fx:Declarations>
<fx:Script>
<![CDATA[
private function init():void{
nav1.activeView.addEventListener(CustomEvent.DATA, onData);
nav2.activeView.addEventListener(CustomEvent.DATA, onData);
}
private function onData(ev:CustomEvent):void{
nav1.activeView.data = ev.data;
nav2.activeView.data = ev.data;
}
]]>
</fx:Script>
<s:ViewNavigator id="nav1" label="Nav1" firstView="views.Nav1Home" width="100%" height="100%"/>
<s:ViewNavigator id="nav2" label="Nav2" firstView="views.Nav2Home" width="100%" height="100%"/>
<强> CustomEvent.as 强>
package
{
import flash.events.Event;
public class CustomEvent extends Event
{
public static var DATA:String = "DATA_EVENT";
public var data:Object = null;
public function CustomEvent(data:Object, type:String, bubbles:Boolean=false, cancelable:Boolean=false)
{
super(type, bubbles, cancelable);
this.data = data;
}
}
}
<强> views.Nav1Home.mxml 强>
<?xml version="1.0" encoding="utf-8"?>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Script>
<![CDATA[
private function init(): void {
this.addEventListener(FlexEvent.REMOVING, removedHandler);
}
private function removedHandler(event:Event):void {
trace("Removed from stage: " + data);
this.dispatchEvent(new CustomEvent("Data from Nav1 Event", CustomEvent.DATA));
}
]]>
</fx:Script>
<强> views.Nav2Home.mxml 强>
<?xml version="1.0" encoding="utf-8"?>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Script>
<![CDATA[
private function init(): void {
this.addEventListener(FlexEvent.REMOVING, removedHandler);
}
private function removedHandler(event:Event):void {
trace("Removed from stage: " + data);
this.dispatchEvent(new CustomEvent("Data from Nav2 Event", CustomEvent.DATA));
}
]]>
</fx:Script>
答案 4 :(得分:0)
如果使用viewNavigator在视图之间进行转换,最简单的方法就是将数据对象传递给视图:
View1中的:
private function view1_clickHandler(event:Event):void {
var trans:FlipViewTransition = new FlipViewTransition();
var obj:Object = new Object();
obj.showPrevBtn = false;
FlexGlobals.topLevelApplication.tabNavigator.pushView(View2, obj, null, trans);
}
View2中的:
protected function view2_addedToStageHandler(event:Event):void
{
if(prevBtn != null && this.data != null && this.data.showPrevBtn === true) {
prevBtn.visible = true;
} else if(prevBtn != null && this.data != null && this.data.showPrevBtn === false) {
prevBtn.visible = false;
}
}