1

我是一个完整的 Flex/Flash 菜鸟,正在运行 Adob​​e Flash Builder 4 Beta 2。我有一个主要组件需要能够调用多个弹出窗口,除了一个功能和几个标签之外,每个都几乎相同。显然,我更希望能够在调用弹出窗口时定义此函数并更改这些标签,而不是拥有大量具有几乎相同代码的 .mxml 文件,我只是不知道该怎么做。我想出了如何更改标签,但不确定如何重新定义功能。

为简单起见,假设我的代码如下所示:

主.mxml:

<?xml version="1.0" encoding="utf-8"?>
    <mx:Module xmlns:fx="http://ns.adobe.com/mxml/2009" :s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/halo" creationComplete="init()">
        <fx:Script>
            <![CDATA[
                import mx.controls.Alert;
                import mx.managers.PopUpManager;

                protected function init():void
                {
                    var alertWindow:IFlexDisplayObject = PopUpManager.createPopUp(FlexGlobals.topLevelApplication as DisplayObject, popup, true);
                    PopUpManager.centerPopUp(alertWindow);
                    var popInstance:transmitRoundPop = alertWindow as transmitRoundPop;
                    popInstance.btnTest.label = "NEW";
                }
            ]]>
        </fx:Script>
    </mx:Module>

弹出.mxml:

<?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/halo" width="400" height="300" xmlns:flash="services.flash.*">
        <fx:Script>
            <![CDATA[  
                import mx.controls.Alert;  
                import mx.managers.PopUpManager;  

                public function test():void
                {
                    Alert.show("ORIGINAL");
                    PopUpManager.removePopUp(this);
                }
            ]]>
        </fx:Script>
        <s:Panel x="10" y="10" width="380" height="280" title="Test" id="pnlTest">
                <s:Button x="131" y="104" label="OLD" id="btnTest" click="test()"/>
        </s:Panel>
    </s:Group>

现在说我想test()在 main.mxml 中调用它时更改 popup.mxml ......我该怎么做?请提供详细信息...记住我是菜鸟:-)

4

2 回答 2

1

当然,在发布问题后,这个想法立即出现在我的脑海中。我想我会在这里发布解决方案,而不是仅仅删除问题,以防它帮助其他人。

我只是test()从 popup.mxml 中完全取出并在 main.mxml 中修改init()为如下所示:

protected function init():void
{
    var alertWindow:IFlexDisplayObject = PopUpManager.createPopUp(FlexGlobals.topLevelApplication as DisplayObject, popup, true);
    PopUpManager.centerPopUp(alertWindow);
    var popInstance:transmitRoundPop = alertWindow as transmitRoundPop;
    popInstance.btnTest.label = "NEW";
    popInstance.btnTest.addEventListener("click", function():void { Alert.show("REDEFINED"); });
}
于 2010-02-01T17:54:12.927 回答
0

如果我得到您的要求,您可以将 test() 设为函数变量并为其提供公共访问权限,以便其他组件可以更改它。

    <fx:Script>
        <![CDATA[  
            import mx.controls.Alert;  
            import mx.managers.PopUpManager;  

            // "test" is now a function variable, 
            // you change it just like any other variable, 
            // but you can call it as well, just like before.
            public var test:Function;

            public function defaultTest():void
            {
                Alert.show("ORIGINAL");
                PopUpManager.removePopUp(this);
            }

            protected function init():void
            {
            // Setting a default value for test
            // otherwise it would give you an error when calling
            // an unassigned function variable.
                this.test = defaultTest; 
                ...
            }
        ]]>
    </fx:Script>

现在在另一个组件中:

public function mainTest():void
{
    ...
}

...
myPopup.test = mainTest;  // setting the variable, note that there are no parentheses.
myPopup.test();   // running the function, note that there are now parentheses.
于 2010-02-01T17:52:53.833 回答