【问题标题】:Flex Mobile Listen for Event in <s:View>Flex Mobile 监听 <s:View> 中的事件
【发布时间】:2012-05-19 03:34:57
【问题描述】:

Flex Mobile 应用程序中是否有一种方法可以监听 &lt;s:View&gt; 的事件,该 &lt;s:ViewNavigator&gt; 位于 &lt;s:ViewNavigator&gt; 内?假设我有以下应用程序结构:


主要应用:

<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>

view.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>

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 事件。我怎样才能听到这样的事件?

感谢您的宝贵时间。

【问题讨论】:

    标签: android ios flash apache-flex flex4


    【解决方案1】:

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

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

    initialize="attachNavigationListeners(event)"
    

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

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

    然后我们需要在导航器完成时添加视图侦听器,我有这个单独的所以你可以在这里拥有尽可能多的视图,可以使用 switch 语句:

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

    为相关视图添加监听器:

    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 :)
    }
    

    注意

    显然,我只是概述了此处所需的所有步骤,我还没有提供完整的复制和粘贴示例,但是你会知道你在做什么来问这个问题,我希望这对你也有帮助已经展示了如何从您的视图中调度事件。

    我也使用过字符串“happened”,但你会有一个像 CustomEvent.HAPPENED 这样的 const 或适合你的东西,以避免以这种方式使用字符串。

    【讨论】:

      猜你喜欢
      • 2010-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-14
      • 1970-01-01
      相关资源
      最近更新 更多