如何在 Flex 中调用具有附加功能的自定义组件
我创建了一个自定义组件(名为customtitlewindow),其代码如下:
<?xml version="1.0" encoding="utf-8"?>
<mx:TitleWindow xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" layout="vertical" width="400" height="300"
xmlns:comp="components.*"
showCloseButton="true"
keyDown="detectescapekeypress(event)"
creationComplete="this.setFocus();"
close="PopUpManager.removePopUp(this);"
paddingTop="40">
<fx:Script>
<![CDATA[
import mx.managers.PopUpManager;
public function detectescapekeypress(event:KeyboardEvent):void
{
if(event.charCode == Keyboard.ESCAPE)
{
PopUpManager.removePopUp(this);
}
}
]]>
</fx:Script>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
</mx:TitleWindow>
现在我再次创建了一个组件(名为deleteconfirm),它像这样调用上面的组件:(
<?xml version="1.0" encoding="utf-8"?>
<mx:Container 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" xmlns:components="components.*">
<fx:Script>
<![CDATA[
import mx.controls.Alert;
]]>
</fx:Script>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<components:customtitlewindow title="custom title window">
<s:Label>
<s:text>this is the custom text for deleteconfirm.</s:text>
</s:Label>
<s:Button label="ok">
<s:click>
<![CDATA[
Alert.show("Hello world!", "title");
]]>
</s:click>
</s:Button>
</components:customtitlewindow>
</mx:Container>
在主文件中)现在单击一个按钮,我调用上述(第二个)如下:
<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
import mx.managers.PopUpManager;
]]>
</mx:Script>
<mx:VBox width="100%" height="100%">
<mx:Button label="Delete Record">
<mx:click>
<![CDATA[
var ctd:deleteconfirm = new deleteconfirm();
ctd = deleteconfirm(PopUpManager.createPopUp(this, deleteconfirm, true));
]]>
</mx:click>
</mx:Button>
</mx:VBox>
</mx:WindowedApplication>
我的主要目的是,对于向最终用户显示的所有弹出窗口,当按下转义键时,所有这些窗口都会关闭,并且当单击显示在的关闭按钮时,所有窗口都会关闭标题栏。
但按“退出键”却没有任何反应。
我该怎么做?
怎么了?以上有错误的地方请指正。
谢谢
I have created a custom component (named customtitlewindow) the code of which is as follows:
<?xml version="1.0" encoding="utf-8"?>
<mx:TitleWindow xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" layout="vertical" width="400" height="300"
xmlns:comp="components.*"
showCloseButton="true"
keyDown="detectescapekeypress(event)"
creationComplete="this.setFocus();"
close="PopUpManager.removePopUp(this);"
paddingTop="40">
<fx:Script>
<![CDATA[
import mx.managers.PopUpManager;
public function detectescapekeypress(event:KeyboardEvent):void
{
if(event.charCode == Keyboard.ESCAPE)
{
PopUpManager.removePopUp(this);
}
}
]]>
</fx:Script>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
</mx:TitleWindow>
Now I again created a component (named deleteconfirm) which calls the above one like this:
<?xml version="1.0" encoding="utf-8"?>
<mx:Container 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" xmlns:components="components.*">
<fx:Script>
<![CDATA[
import mx.controls.Alert;
]]>
</fx:Script>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<components:customtitlewindow title="custom title window">
<s:Label>
<s:text>this is the custom text for deleteconfirm.</s:text>
</s:Label>
<s:Button label="ok">
<s:click>
<![CDATA[
Alert.show("Hello world!", "title");
]]>
</s:click>
</s:Button>
</components:customtitlewindow>
</mx:Container>
(in main file) Now on the click of a button I am calling the above (second one) as follows:
<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
import mx.managers.PopUpManager;
]]>
</mx:Script>
<mx:VBox width="100%" height="100%">
<mx:Button label="Delete Record">
<mx:click>
<![CDATA[
var ctd:deleteconfirm = new deleteconfirm();
ctd = deleteconfirm(PopUpManager.createPopUp(this, deleteconfirm, true));
]]>
</mx:click>
</mx:Button>
</mx:VBox>
</mx:WindowedApplication>
The main purpose of mine is that for all the popup windows shown to end user all of them be closed when the escape key is pressed and all of them be closed upon click of the closebutton displayed at the titlebar.
But on the "escape key" press nothing is happening.
How can I do this?
What is wrong? Please correct me where ever I am wrong above.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试为您的deleteconfirm类执行此操作:
此外,您应该考虑遵守适当的标准,例如类上的大写(而不是deleteconfirm,它应该是DeleteConfirm;并且更具描述性也无害)。
Try doing this for your deleteconfirm class:
Also, you should look into adhering to proper standards like uppercasing on classes (instead of deleteconfirm, it should be DeleteConfirm; and it wouldn't hurt to be more descriptive).
您正在尝试删除错误的弹出引用。
您正在创建的 popUp 是 deleteconfirm 类的实例,但是当您尝试在 detectescapekeypress() 函数上删除它时,您正在从 deleteconfirm 类传递一个实例。 em>customtitlewindow 类。
修复此问题的简单方法是更改 Detectescapekeypress() 中的这一行:
修复此问题的最佳方法是移动deleteconfirm 类的按键处理。
You are trying to remove the wrong popUp reference.
The popUp that you are creating is an instance of the deleteconfirm class but when you try to remove it, on the detectescapekeypress() function, you are passing an instance from a customtitlewindow class.
An easy way to fix it is by changing this line in the detectescapekeypress():
The best way to fix it, would be to move the key-press handling to the deleteconfirm class.