自定义 Flex 组件中未触发事件

发布于 2024-11-17 05:43:28 字数 1711 浏览 4 评论 0原文

我第一次尝试通过扩展 UIComponent 类来编写自定义 Flex 4 组件。不幸的是,我无法让组件响应任何类型的鼠标事件。我尝试将组件的 mouseEnabled 设置为 true,并在父组件(舞台对象)中将 mouseChildren 设置为 true。

看来无论我做什么,我的点击事件都可以从舞台上检测到,但不能通过组件检测到。

这是我的组件类:

package components {

    import mx.core.UIComponent;

    public class DrawCanvas extends UIComponent {

        public function DrawCanvas() {
            super();
        }
    }
}

这是我的 WindowedApplication 文件:

<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
                       xmlns:s="library://ns.adobe.com/flex/spark" 
                       xmlns:mx="library://ns.adobe.com/flex/mx"
                       xmlns:cmp="components.*"
                       minWidth="800" minHeight="600"
                       applicationComplete="init()">

    <fx:Script>
        <![CDATA[
            private function init():void {
                myBox.addEventListener(MouseEvent.CLICK, reportClick);
                stage.addEventListener(MouseEvent.CLICK, stageClick);
            }

            private function stageClick(event:MouseEvent):void {
                trace(event.target, event.currentTarget);
                trace("Stage Click", event.localX, event.localY);
            }

            private function reportClick(event:MouseEvent):void {
                trace(event.target, event.currentTarget);
                trace("Click", event.localX, event.localY);
            }
        ]]>
    </fx:Script>

        <cmp:DrawCanvas id="myBox"
                    height="100%" width="100%"/>

</s:WindowedApplication>

提前致谢,

Sam

I'm taking my first stab at writing a custom flex 4 component by extending the UIComponent class. Unfortunately, I cannot get the component to respond to any sort of mouse events. I've tried setting mouseEnabled to true is the component, as well as setting mouseChildren to true in the parent (the stage object).

It seems whatever I do, my click events can be detected from the stage, but not with the component.

Here is my component class:

package components {

    import mx.core.UIComponent;

    public class DrawCanvas extends UIComponent {

        public function DrawCanvas() {
            super();
        }
    }
}

And here is my WindowedApplication file:

<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
                       xmlns:s="library://ns.adobe.com/flex/spark" 
                       xmlns:mx="library://ns.adobe.com/flex/mx"
                       xmlns:cmp="components.*"
                       minWidth="800" minHeight="600"
                       applicationComplete="init()">

    <fx:Script>
        <![CDATA[
            private function init():void {
                myBox.addEventListener(MouseEvent.CLICK, reportClick);
                stage.addEventListener(MouseEvent.CLICK, stageClick);
            }

            private function stageClick(event:MouseEvent):void {
                trace(event.target, event.currentTarget);
                trace("Stage Click", event.localX, event.localY);
            }

            private function reportClick(event:MouseEvent):void {
                trace(event.target, event.currentTarget);
                trace("Click", event.localX, event.localY);
            }
        ]]>
    </fx:Script>

        <cmp:DrawCanvas id="myBox"
                    height="100%" width="100%"/>

</s:WindowedApplication>

Thanks in advance,

Sam

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

安静被遗忘 2024-11-24 05:43:28

组件只能通过可见部分调度鼠标事件。只要您的组件没有任何内容,它就无法触发鼠标事件。尝试类似的方法:

package components {

    import mx.core.UIComponent;

    public class DrawCanvas extends UIComponent {

        public function DrawCanvas() {
            super();
        }

        override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
        {
            super.updateDisplayList(unscaledWidth, unscaledHeight);
            graphics.clear();
            graphics.beginFill(0xFFFFFF, 0);
            graphics.drawRect(0, 0, unscaledWidth, unscaledHeight);
            graphics.endFill();
        }
    }
}

Component can only dispatch mouse events only by visible parts. As far as your component hasn't any content it can't fire mouse events. Try something like:

package components {

    import mx.core.UIComponent;

    public class DrawCanvas extends UIComponent {

        public function DrawCanvas() {
            super();
        }

        override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
        {
            super.updateDisplayList(unscaledWidth, unscaledHeight);
            graphics.clear();
            graphics.beginFill(0xFFFFFF, 0);
            graphics.drawRect(0, 0, unscaledWidth, unscaledHeight);
            graphics.endFill();
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文