android - Flex Mobile 监听 <s :View> 中的事件

标签 android ios flash apache-flex flex4

Flex Mobile 应用程序中是否有一种方法可以监听 <s:View> 的事件这是在 <s:ViewNavigator> 内?假设我有以下应用程序结构:

<小时/>

主要应用:

<s:TabbedViewNavigatorApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
                  xmlns:s="library://ns.adobe.com/flex/spark"
                  creationComplete="databaseConnection(event)">

  <s:ViewNavigator id="tasks" width="100%" height="100%"
                       label="Tasks" firstView="views.TasksView"
                       title="Tasks" icon="@Embed('assets/icons/tasks.png')">
  </s:ViewNavigator>
</s:TabbedViewNavigatorApplication>

查看.TasksView:

<s:View xmlns:fx="http://ns.adobe.com/mxml/2009" 
        xmlns:s="library://ns.adobe.com/flex/spark">

  <s:Button label="New View" click="{navigator.pushView(views.AddTask)}"/>
</s:View>

查看.AddTask:

<s:View xmlns:fx="http://ns.adobe.com/mxml/2009" 
        xmlns:s="library://ns.adobe.com/flex/spark"
                creationComplete="{dispatchEvent(new Event("happened"))}">

  <fx:Metadata>
    [Event(name="happened", type="flash.events.Event")]
  </fx:Metadata>
</s:View>

假设我想听 happened事件回到我的主应用程序中。我怎样才能监听这样的事件?

感谢您的宝贵时间。

最佳答案

是的,可以,可以这样做:

首先在您的应用程序中将此属性添加到您的根标记中:

initialize="attachNavigationListeners(event)"

下一个方法将向需要自定义事件的导航器添加一个完整的事件:

private function attachNavigationListeners(event : FlexEvent) : void {
    navigator.addEventListener(Event.COMPLETE,attachViewListeners);
}

然后我们需要在导航器完成时添加 View 监听器,我将其分开,以便您可以根据需要在此处拥有任意多个 View ,可以使用 switch 语句:

private function attachViewListeners(event : Event) : void {
    if(navigator.activeView is FirstView) {
          addListenersToFirstView();
    }
}

向相关 View 添加监听器:

private function addListenersToFirstView() : void{
    if(navigator.activeView is Firstview) {
          var view: Firstview = navigator.activeView);
          view.addEventListener("happened", handleHappened);
     }
}

最终处理事件:

private function handleHappened(event:Event) : void{
    // I hope something really did happen :)
}

注意

显然,我刚刚概述了这里所需的所有步骤,我没有提供完整的工作示例来复制和粘贴,但是您会知道您在做什么来问这个问题,我希望这可以帮助您,也可以帮助您已经展示了如何从您的 View 中分派(dispatch)事件。

我还使用了字符串“happened”,但您将拥有像 CustomEvent.HAPPENED 这样的常量或适合您的常量,以避免以这种方式使用字符串。

关于android - Flex Mobile 监听 <s :View> 中的事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10662096/

相关文章:

ios - 打开控制中心时CGImageCreate : invalid image size 0 x 0,

flash - Actionscript 对象的属性数

Java servlet 和从 Flash 的 xml.sendAndLoad() 函数接收 XML

iphone - 为 iPhone Flash 应用程序优化 tweenlite

android - 暂停Android模拟器的方法?

android - Couchbase 精简版 : reading document vs querying data

java - 有什么方法可以从 Android Studio 将数据写入 json 吗?

ios - Multipeer Connectivity 存储以前 session 中的 PeerID

ios - 在 swift 中为 firebase 自定义登录

android - Dagger2 不生成 Dagger* 类