【问题标题】:how do I call a customcomponent with added functionality in flex如何在 flex 中调用具有附加功能的自定义组件
【发布时间】:2011-04-08 10:29:01
【问题描述】:

我创建了一个自定义组件(名为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>

我的主要目的是,所有显示给最终用户的弹出窗口都在按下退出键时关闭,并且在点击标题栏上显示的关闭按钮时全部关闭。

但是在“退出键”上按什么都没有发生。
我该怎么做?
怎么了?请纠正我上面的错误。

谢谢

【问题讨论】:

    标签: apache-flex mxml code-reuse custom-component reusability


    【解决方案1】:

    尝试为您的 deleteconfirm 类执行此操作:

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

    此外,您应该考虑遵守适当的标准,例如在类上大写(而不是 deleteconfirm,它应该是 DeleteConfirm;更具描述性也无妨)。

    【讨论】:

    • +1 用于遵守大写标准和描述性命名。 :)
    • 单击删除按钮时出现此错误:“已为此组件指定了多组可视子项(基本组件定义和派生组件定义)。”对此进行了谷歌搜索,我认为这是不可能的。有什么想法吗?
    • 尝试这样做:&lt;s:Button label="ok" click="Alert.show('Hello world!', 'title')" /&gt;
    【解决方案2】:

    您正在尝试删除错误的弹出引用。

    您正在创建的弹出窗口是 deleteconfirm 类的一个实例,但是当您尝试删除它时,在 detectescapekeypress() 函数中,您传递的是一个实例来自 customtitlewindow 类。

    修复它的简单方法是通过更改 detectescapekeypress() 中的这一行:

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

    修复它的最佳方法是将按键处理移至 deleteconfirm 类。

    【讨论】:

    • 感谢您的回答,但我认为您的解决方案会破坏我的问题的目的。我不想重复 escapekeysequence 的代码。
    猜你喜欢
    • 1970-01-01
    • 2012-04-26
    • 1970-01-01
    • 1970-01-01
    • 2016-10-26
    • 1970-01-01
    • 2013-02-14
    • 2016-12-30
    • 2011-01-11
    相关资源
    最近更新 更多