有没有像 MXML 接口这样的东西 [英] is there such a thing as an MXML interface

查看:30
本文介绍了有没有像 MXML 接口这样的东西的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个愚蠢的问题,如果是,请提前道歉.我想知道 MXML 中是否有等效的接口?

This could potentially be a dumb question so apologies in advance if it is. I'm wondering if theres an equivilant of Interfaces in MXML?

每次我觉得需要使用界面时,我总是会制作一个动作脚本而不是 MXML 文件,因为我不知道您是否/如何可以.

Everytime I feel the need to use an interface I always wind up making an actionscript and not an MXML file because I don't know if / how you can.

例如,我将有一个基于 vbox 的组件.我对同一件事有 4 种不同的实现,所以我决定使用一个接口.但是我没有制作单个 MXML 接口并实现它,而是在 as3 中创建了一个接口.我已经在 4 个不同的类中实现了这个接口.

For example I was going to have a component based on vbox. I have 4 different implementions of the same thing so I decided to use an interface. But instead of making a single MXML interface and implementing it I've created an interface in as3. I've implemented this interface in 4 different classes.

然后我制作了 4 个不同的 vbox 容器,每个容器都在脚本标签中使用了一种不同的实现.

I then have made 4 different vbox containers each with one of the different implementations in the script tag.

这听起来是一个合理的方法还是我在这里违背了原则?

Does this sound like a reasonable approach or am I going against the grain here?

编辑——添加示例

界面

package components.content.contents
{
    public interface IContent
    {
        function init():void;
        function doSearch():void
        function setSearchTerm(term:String):void
    }
}

实施(4 个中的 1 个)

package components.content.contents
{
    public class ClipContent extends AbstractContent implements IContent
    {
        public function ClipContent()
        {
        }

        public function init():void
        {
        }

        public function doSearch():void
        {
        }

        public function setSearchTerm(term:String):void
        {
        }

    }
}

MXML 文件(4 个中的 1 个)

<?xml version="1.0" encoding="utf-8"?>
<mx:VBox xmlns:mx="http://www.adobe.com/2006/mxml" width="400" height="300">
        <mx:Script>
            <![CDATA[
                              // ClipContent Container
                import components.content.contents.ClipContent;
                public var content:ClipContent= new ClipContent()

                public function dostuff():void

                {
                  content.init()
                  content.doSearch()

                }
            ]]>
        </mx:Script>

</mx:VBox>

推荐答案

您可以通过以下方式将接口与 MXML 组件一起使用:

You can use interfaces with MXML components this way:

// YourClass.mxml
<mx:HBox implements="IYourInterface">

是一个 MXML 等价物

is an MXML equivalent of

// YourClass.as
class YourClass extends HBox implements IYourInterface

但是您仍然需要在 Actionscript 中创建接口(在本例中为 IYourInterface).

But you still need to create the interface (in this example IYourInterface) in Actionscript.

这篇关于有没有像 MXML 接口这样的东西的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆