如何在MXML中使用AS3类?
我如何在 MXML 中使用以下 AS3 类?
AS3 类:
package mtm
{
import flash.display.MovieClip;
import flash.display.Shape;
public class TestClass extends MovieClip
{
public function TestClass()
{
var s:Shape = new Shape();
s.graphics.beginFill(0x000000, 1);
s.graphics.drawRect(0, 0, 60, 60);
s.graphics.endFill();
addChild(s);
}
}
}
MXML 文档:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Panel width="75%" height="75%" paddingTop="10" paddingLeft="10">
</mx:Panel>
</mx:Application>
我需要声明自己的命名空间吗?我假设可以做类似的事情:
//Where 'mtm' is my own namespace
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:mtm="com.mtm.*"></mx:Application>
然后做这样的事情?
<mtm:TestClass></mtm:TestClass>
我对 Flex 和 MXML 很陌生,但对 AS3 并不陌生。 谢谢!
How would I use the following AS3 class within MXML?
AS3 Class:
package mtm
{
import flash.display.MovieClip;
import flash.display.Shape;
public class TestClass extends MovieClip
{
public function TestClass()
{
var s:Shape = new Shape();
s.graphics.beginFill(0x000000, 1);
s.graphics.drawRect(0, 0, 60, 60);
s.graphics.endFill();
addChild(s);
}
}
}
MXML Document:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Panel width="75%" height="75%" paddingTop="10" paddingLeft="10">
</mx:Panel>
</mx:Application>
Do I need to declare my own namespace? I am assuming it is possible to do something like:
//Where 'mtm' is my own namespace
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:mtm="com.mtm.*"></mx:Application>
And then do something like this?
<mtm:TestClass></mtm:TestClass>
I'm new to Flex and MXML, but not new to AS3.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,你的想法是正确的。您的自定义 xmlns 是一个相对 url,指向自定义组件类,因此如果 TestClass 位于名为 Components 的文件夹中,您将放置
xmlns:mtm="components.*"
。您的 MXML 是正确的。这是相关的 LiveDocs 链接。这是查找 MXML/AS3 信息的好地方:
http://livedocs.adobe.com/flex/ 3/html/help.html?content=intro_3.html
Yes, you have the right idea. Your custom xmlns is a relative url, pointing to the custom component classes, so if TestClass is in a folder called Components, you would put
xmlns:mtm="components.*"
. Your MXML is correct.Here is the relevant LiveDocs link. This is a good place to find MXML/AS3 information:
http://livedocs.adobe.com/flex/3/html/help.html?content=intro_3.html
在我看来,你的问题已经有了答案。
是的,您必须完全按照您所做的那样声明您自己的名称空间;通过在示例应用程序的顶级标签上指定它。
是的,然后您可以使用该名称空间引用您的类,就像您在示例中所述一样。
您有具体问题吗?由于您的自定义组件没有扩展 UIComponent;我怀疑你可能会遇到一些奇怪的事情;因为您的组件不会有 Flex 组件生命周期方法,例如 createChildren()、commitProperties() 和 updateDisplayList(),这可能会混淆 Flex 管理器类。
It seems to me like your question already has your answer.
Yes, you have to declare your own namespace exactly as you have done; by specifying it on the top level tag--in your example Application.
And yes then you can reference your class using that namespace, just like you stated in your sample.
Are you having a specific problem? Since your custom component is not extending UIComponent; I suspect you may run into some oddities; as your component wwont' have Flex Component LifeCycle methods such as createChildren(), commitProperties(), and updateDisplayList() which will probably confuse the Flex manager classes.