![](/img/trans.png)
[英]How can I define custom 'contentGroups' in a custom Flex 4 component?
[英]Flex - How can I define a function in one component from another?
我是一个完整的Flex / Flash新手,运行Adobe Flash Builder 4 Beta2。我有一个主要组件,它需要能够调用多个弹出窗口,每个弹出窗口除了一个功能和几个标签外几乎都是相同的。 显然,我更希望能够定义此函数并在调用弹出窗口时更改这些标签,而不是拥有大量使用几乎相同的代码的.mxml文件,我只是不知道该怎么做。 我想出了如何更改标签的方法,但不确定如何重新定义功能。
为了简单起见,假设我的代码如下所示:
main.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>
popup.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>
现在说我想在main.mxml中调用它时在popup.mxml中更改test()
...我该怎么做? 请提供详细信息...记住我是菜鸟:-)
当然,在发布问题后,这个想法立即浮现在我的脑海。 我以为我会在这里发布解决方案,而不是仅仅删除问题,以防其他人受到影响。
我只是从popup.mxml中完全取出test()
,并在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"); });
}
如果我得到您的要求,可以使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.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.