how do I call a customcomponent with added functionality in flex

前提是你 提交于 2019-12-23 04:57:05

问题


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


回答1:


Try doing this for your deleteconfirm class:

<components:customtitlewindow 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.*" title="custom title window">
    <fx:Script>
        <![CDATA[
            import mx.controls.Alert;
        ]]>
    </fx:Script>
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>

        <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>

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).




回答2:


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():

PopUpManager.removePopUp(IFlexDisplayObject(this.parent));

The best way to fix it, would be to move the key-press handling to the deleteconfirm class.



来源:https://stackoverflow.com/questions/5593796/how-do-i-call-a-customcomponent-with-added-functionality-in-flex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!