Flex 4:从组件访问主应用程序中的公共方法

时间:2010-09-13 14:30:18

标签: flex actionscript-3 actionscript flex4

我需要能够从Flex 4中主应用程序下的组件调用方法。有人可以告诉我如何在不使用FlexGlobals的情况下执行此操作吗?

附上示例代码。提前谢谢。

// TestApp.mxml (application)
<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
                       xmlns:s="library://ns.adobe.com/flex/spark" 
                       xmlns:mx="library://ns.adobe.com/flex/mx"
                       creationComplete="initApp()">
    <fx:Script>
        <![CDATA[
            import com.TestComp;

            import mx.managers.PopUpManager;

            public function myMethod():void
            {
                // do something
            }

            protected function initApp():void
            {
                var popUp:TestComp = new TestComp();

                PopUpManager.addPopUp(popUp, this, true);
            }
        ]]>
    </fx:Script>
</s:WindowedApplication>

// TestComp.mxml (component)
<?xml version="1.0" encoding="utf-8"?>
<s:Group xmlns:fx="http://ns.adobe.com/mxml/2009" 
         xmlns:s="library://ns.adobe.com/flex/spark" 
         xmlns:mx="library://ns.adobe.com/flex/mx"
         width="400" height="300">
    <fx:Script>
        <![CDATA[
            private function doSomething(event:MouseEvent):void
            {
                // call to myMethod() in TestApp.mxml
            }
        ]]>
    </fx:Script>
    <s:Button click="doSomething(event)" label="Click Me"/>
</s:Group>

3 个答案:

答案 0 :(得分:2)

这是糟糕的设计。您应该提供回调函数或事件监听器。

// TestComp.mxml

<mx:Metadata>
  [Event(name="doSomethingEvent", type="flash.events.Event")]
</mx:Metadata>

<mx:Script><![CDATA[

  private function doSomething(event:MouseEvent):void
  {
    this.dispatchEvent(new Event("doSomethingEvent"));
  }

]]></mx:Script>

// TestApp.mxml
protected function initApp():void
{
  var popUp:TestComp = new TestComp();
  popUp.addEventListener("doSomethingEvent", myMethod);
  PopUpManager.addPopUp(popUp, this, true);
}

private function myMethod(event: Event): void
{
  // do something
}

这是一个回调示例:

// TestComp.mxml

public var doSomethingCallback: Function;

private function doSomething(event:MouseEvent):void
{
  doSomethingCallback.call();
}

// TestApp.mxml
protected function initApp():void
{
  var popUp:TestComp = new TestComp();
  popUp.doSomethingCallback = myMethod;
  PopUpManager.addPopUp(popUp, this, true);
}

private function myMethod(): void
{
  // do something
}

答案 1 :(得分:1)

最简单的选择?

从TestComp中的按钮中取出点击处理程序。

在主应用程序中,为MouseEvent.CLICK添加一个侦听器(如果它是主应用程序的直接子节点)或自身(如果TestComp位于显示列表的下方)。在处理程序中,测试事件的目标是否是TestComp,通过==如果你有直接引用,或者通过“是”,如果没有。

这是你刚才所做的最少的努力,仍然依赖于(冒泡)事件,而且更“正确”

答案 2 :(得分:0)

我确实同意它的设计不好,但以下情况应该有效

//in TestApp
protected function initApp():void
{
  var popUp:TestComp = new TestComp(this);
  PopUpManager.addPopUp(popUp, this, true);
}

//in TestComp
private var app:TestApp;

public function TestComp(app:TestApp)
{
  this.app = app;
}

private function doSomething(event:MouseEvent):void
{
  // call to myMethod() in TestApp.mxml
  app.myMethod();
}

或者你可以这样做

//in TestApp
protected function initApp():void
{
  var popUp:TestComp = new TestComp(this);
  popUp.addEventListener( 'test' , eventListener );
  PopUpManager.addPopUp(popUp, this, true);
}
private function eventListener(event:Event):void
{ 
   //in which case myMethod doesn't need to be public
   myMethod();
}

//in TestComp
private function doSomething(event:MouseEvent):void
{
  dispatchEvent( new Event('test') );
}