在 Flash Builder 的自定义组件中,应将自定义代码放在哪里?

发布于 2024-10-07 08:09:06 字数 337 浏览 6 评论 0 原文

在主文件中,我会写:

<components:mybutton id="mybutton1" rollOver="point_rollOverHandler(event)" />

但是,如果我希望组件本身就有这种行为,我应该在 mybutton mxml 文件中的哪里编写它以使其引用自身?

我尝试了 但它抱怨元素类型必须后跟任一属性规范,>或 />

in the main file, I would write:

<components:mybutton id="mybutton1" rollOver="point_rollOverHandler(event)" />

But if I want the component to have that behaviour innately, where do I write it in the mybutton mxml file to have it reference itself?

I tried <s:rollOver="point_rollOverHandler(event)"/> but it complains the element type must be followed by either atrribute specifications, > or />

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

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

发布评论

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

评论(2

太阳男子 2024-10-14 08:09:06

我的猜测是您的组件基于 s:Button。只需在组件主 MXML 节点中声明 rollOver 方法,如下所示(第 5 行):

// myButton.mxml

<?xml version="1.0" encoding="utf-8"?>
<s:Button xmlns:fx="http://ns.adobe.com/mxml/2009" 
          xmlns:s="library://ns.adobe.com/flex/spark" 
          xmlns:mx="library://ns.adobe.com/flex/mx"
          rollOver="button1_rollOverHandler(event)"

          >

    <fx:Script>
        <![CDATA[
            protected function button1_rollOverHandler(event:MouseEvent):void
            {
                // TODO Auto-generated method stub
            }
        ]]>
    </fx:Script>

    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>
</s:Button>

My guess is that your component is based on s:Button. Just declare the rollOver method in your components main MXML node like this (line 5):

// myButton.mxml

<?xml version="1.0" encoding="utf-8"?>
<s:Button xmlns:fx="http://ns.adobe.com/mxml/2009" 
          xmlns:s="library://ns.adobe.com/flex/spark" 
          xmlns:mx="library://ns.adobe.com/flex/mx"
          rollOver="button1_rollOverHandler(event)"

          >

    <fx:Script>
        <![CDATA[
            protected function button1_rollOverHandler(event:MouseEvent):void
            {
                // TODO Auto-generated method stub
            }
        ]]>
    </fx:Script>

    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>
</s:Button>
小鸟爱天空丶 2024-10-14 08:09:06

在自定义组件的根标记中添加属性 :

creationComplete="init()"

然后在自定义组件的脚本标记中创建该函数并设置鼠标事件侦听器:

function init():void{
  this.addEventListener(MouseEvent.MOUSE_OVER, point_rollOverHandler)
}

但这将调用自定义组件中定义的函数 point_rollOverHandler(e:MouseEvent) 。如果您想调用在其父级上定义的函数,那么您拥有的就是最好的方法。否则,您会将组件捆绑得太紧,这将使您的代码变得脆弱并且可重用性降低。

in the root tag for your custom component add the property :

creationComplete="init()"

then in the script tag of your custom component create that function and set up the mouse event listener:

function init():void{
  this.addEventListener(MouseEvent.MOUSE_OVER, point_rollOverHandler)
}

but this will call the function point_rollOverHandler(e:MouseEvent) defined in your custom component. If you're looking to call a function defined on it's parent then what you have is the best way. Otherwise you'd be tying the components too tightly together which would make your code brittle and less reusable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文