Flex - 调用位于主视图外部的公共方法

时间:2012-06-10 09:29:17

标签: actionscript-3 flash flex mobile adobe

Flex的新手,我正在试图找到一种方法来调用在视图上声明的公共方法,该视图是应用程序的“firstView”。我如何获得参考?

主视图

<?xml version="1.0" encoding="utf-8"?>
<s:ViewNavigatorApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" applicationDPI="160"  firstView="views.loaderView" creationComplete="init()">
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>
    <fx:Script>
        <![CDATA[
            private function init():void{
                //how do I access this method?
                        loaderView.setLoaderMsg("My Message");
            }
        ]]>
    </fx:Script>
</s:ViewNavigatorApplication>

装载程序视图                                         

    <fx:Declarations>
        <s:RadioButtonGroup id="rg"/>
    </fx:Declarations>

    <s:layout>
        <s:VerticalLayout paddingTop="10" paddingBottom="15" paddingLeft="15" paddingRight="15" gap="15" 
                          horizontalAlign="center" verticalAlign="middle"/>
    </s:layout>
    <s:BusyIndicator/>  
    <s:Label id="loaderMsg" text="Loading..." />

</s:View>

2 个答案:

答案 0 :(得分:3)

默认情况下,MXML中定义的所有子项都在公共命名空间中,因此您可以通过点表示法轻松访问它。这是Flex的第一步最简单的方法。

<?xml version="1.0" encoding="utf-8"?>
<s:ViewNavigatorApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
                            xmlns:s="library://ns.adobe.com/flex/spark" 
                            applicationDPI="160"
                            firstView="views.loaderView"
                            creationComplete="init()">
  <fx:Declarations>
  <!-- Place non-visual elements (e.g., services, value objects) here -->
  </fx:Declarations>
  <fx:Script>
    <![CDATA[
      private function init():void{
        trace(navigator.activeView);
        // const first:loaderView = navigator.activeView as loaderViewM
        // first.loaderMsg.text = "My Message";
      }
    ]]>
  </fx:Script>
</s:ViewNavigatorApplication>

答案 1 :(得分:1)

使用FlexGlobals.topLevelApplication.myPublicFunction