开发者

is there such a thing as an MXML interface

开发者 https://www.devze.com 2023-01-06 01:59 出处:网络
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?

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?

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.

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.

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

Does this sound like a r开发者_StackOverfloweasonable approach or am I going against the grain here?

EDIT -- adding examples

The interface

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

Implementation (1 of 4)

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 File (1 of 4)

<?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>


You can use interfaces with MXML components this way:

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

is an MXML equivalent of

// YourClass.as
class YourClass extends HBox implements IYourInterface

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


MXML can implement an interface, like Robert Bak said, but it cannot define an interface.


No! MXML is a Declarative language for layout and positioning. By definition it needs an implementation. Interfaces are the definition of an API without an implementation.

It sounds like you're doing things exactly how I would. It is perfectly acceptable for an MXML Component to implement an interface. And it is perfectly acceptable to have multiple components implement the same interface to achieve different results.


For the sake of completeness, an MXML Component can implement an interface just like an ActionScript component ca:

<mx:myComp implements="com.myClass.Interface">


You are correct, there is no way to implement a true interface using MXML(edit: I stand corrected, you can use the "implements" keyword as described in the other answers.) Another approach to consider is to use a "code behind" actionscript file for each of your 4 MXML files:

MXML file (MyFancyVBox.mxml):

<?xml version="1.0" encoding="utf-8"?>
<MyFancyVBoxCode>
...
</MyFancyVBoxCode>

AS file (MyFancyVBoxCode.as):

package com.something.whatever
{
    import com.something.another.IFancyInterface;

    public class MyFancyVBoxCode implements IFancyInterface
    {
        ...
    }
}

The downside is that it doubles the number of source files.


Firstly, I agree with Wade that Code behind may help you.

Secondly, I am thinking do you need the interface in you case. In your question, you want "4 different implementions of the same thing". How about using the "state" in the mxml. It may solve you problems.

0

精彩评论

暂无评论...
验证码 换一张
取 消