自定义 Flex 组件中未触发事件
我第一次尝试通过扩展 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
组件只能通过可见部分调度鼠标事件。只要您的组件没有任何内容,它就无法触发鼠标事件。尝试类似的方法:
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: