4

我正在尝试,碰巧我很难弄清楚如何在 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 之间传递数据?我知道如何在导航视图之间做到这一点。

谢谢,B。

4

5 回答 5

2

我想补充一点,与已经讨论过的相比,我更喜欢更松散耦合的通信机制。我更喜欢某种类型的事件聚合系统,其中解耦的组件在彼此不知道彼此的情况下发送消息。

这种类型的事件系统内置在 Parsely 和 Robot Legs 等轻量级框架中。

这主要是一种风格的东西,但根据我的经验,我们越是像这样紧密地结合我们的沟通,我们为它付出的代价就越多。

我想,值得深思。:)

于 2011-02-13T03:26:51.980 回答
2

一种简单的方法是使用 ViewNavigator 将数据对象添加到视图中。然后在子视图中,使用它来获取数据:

this.parentDocument.yourDataObject
于 2011-05-03T03:13:57.357 回答
1

TJ already got it right. Just posting final example in case someone get the same issue:

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>

于 2011-02-13T00:41:40.383 回答
1

除非我在这里遗漏了一些东西,否则您要做的是在这里放置一个脚本块,并听取您的 ViewNavigator 的事件。然后在事件的处理程序中,调用目标 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>
于 2011-02-12T12:39:49.960 回答
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;
  }
}
于 2013-04-06T17:46:46.970 回答