开发者

create interface object in mxml

Let's say I have an interface

public interface IFoo {
   ...
}

and I have several implementing classes

public class Foo implements IFoo {
    ...
}

...

public class Bar implements IFoo {
    ...
}

...

public class Baz implements IFoo {
    ...
}

I want to reference IFoo in MXML like this

<ns:IFoo id="myfoo"/>

and have it be instantiated at runt开发者_StackOverflow社区ime by a factory.

However, the compiler won't let me do this-- it's trying to do "new IFoo" in the generated ActionScript.

How to get around this? How can I use an interface and a factory purely in MXML?


Declaring an MXML child instantiates an object of that type - you can't simply declare a property in MXML without associating an instance with it.

Given that - there's no way to achieve the equivalent of

public var myFoo:IFoo;

in your MXML.

As James pointed out, you can use a ClassFactory to acheive the following:

<mx:ClassFactory class="{Foo}" id="fooFactory" />

but you would be required to call fooFactory.newInstance() to get an IFoo.


You can implement interfaces in MXML components with the implements="IFoo" attribute in the component's root node.

Edit:

Sorry, I missunderstood your question. I don't know a way to implement a factory in pure mxml. I guess you have to use Actionscript or mxml states to achieve a similar behaviour.


Check out ClassFactory. It is how things like item renderers are instantiated.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜