从 MXML 调用 ActionScript 包函数

发布于 2024-12-08 20:06:11 字数 429 浏览 1 评论 0原文

如何从 MXML 调用 btn.as 中的函数?是否可以在不创建 btn 实例的情况下调用函数?

main.mxml 包含一个 Spark 按钮:

<s:Button text="Add Image"/>

btn.as 是一个包:

package {
    public class btn extends Sprite {
        public function btn() {
        }

        public function addImage():void {
           var im:Image = new Image("background.png");
           addChild(im);
        }
    }
}

How can I invoke a function in btn.as from MXML and is it possible to call a function without creating an instance of btn?

main.mxml which contain a Spark button:

<s:Button text="Add Image"/>

btn.as is a package:

package {
    public class btn extends Sprite {
        public function btn() {
        }

        public function addImage():void {
           var im:Image = new Image("background.png");
           addChild(im);
        }
    }
}

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

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

发布评论

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

评论(1

冷月断魂刀 2024-12-15 20:06:12

你到底想要什么有点令人困惑,但我会尝试一下。

如何从 MXML 调用 btn.as 中的函数

我假设您想在单击按钮时在 btn 组件的实例上调用 addImage 函数。你可以这样:

<s:Button text="Add Image" click="{btnInstance.addImage()}"/>

如果你想要别的东西,你就必须详细说明。

是否可以在不创建 btn 实例的情况下调用函数?

是的,将其设为静态方法。像这样的事情:

    package {
        public class btn extends Sprite {
            public function btn() {
            }

            public static function addImage():void {
               var im:Image = new Image("background.png");
               addChild(im);
            }
        }
    }

然后你可以像这样调用静态方法:

<s:Button text="Add Image" click="{btn.addImage()}"/>

需要注意的是,我不希望“addChild”在静态方法中做任何有用的事情。如果没有组件实例,则不在显示列表中;并且您的新“孩子”将永远不会被展示。事实上,没有办法引用新的孩子。我想你可以将一个容器传递给 addImage 函数并在那里添加孩子。从概念上讲是这样的:

            public static function addImage(container:UIComponent):void {
               var im:Image = new Image("background.png");
               container.addChild(im);
            }

不过,我最初对这样的方法持保留态度,因此在没有完全理解用例的情况下不会推荐它。

It is a bit confusing what exactly you're after, but I'll give it a shot.

How can I invoke a function in btn.as from MXML

I'm going to assume you want tocall the addImage function on an instance of the btn component when the button is clicked. You can it like this:

<s:Button text="Add Image" click="{btnInstance.addImage()}"/>

If you want something else, you'll have to elaborate.

is it possible to call a function without creating an instance of btn?

Yes, make it a static method. Something like this:

    package {
        public class btn extends Sprite {
            public function btn() {
            }

            public static function addImage():void {
               var im:Image = new Image("background.png");
               addChild(im);
            }
        }
    }

then you can call the static method like this:

<s:Button text="Add Image" click="{btn.addImage()}"/>

The caveat is that I don't expect "addChild" would do anything useful inside a static method. If there is no component instance, then it isn't on the display list; and your new "child" will never be displayed. In fact, there would be no way to reference the new child. I suppose you could pass in a container to the addImage function and add the child there. Conceptually like this:

            public static function addImage(container:UIComponent):void {
               var im:Image = new Image("background.png");
               container.addChild(im);
            }

I have initial reservations about an approach like that though, so would not recommend it without fully understanding the use case.

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