如何在Flex3/AS3中访问root?
假设我的主 mxml 和这样的函数中有代码:
this.addChild(someContainer);
现在我想重构代码并将其移至单独文件中它自己的类和方法。我现在如何访问root,因为这显然现在指向我创建的新类。
Say I have code in my main mxml and in a function like this:
this.addChild(someContainer);
and now I want to refactor code and move this to it's own class and method in a separate file. How could I access root now, since this obviously now points to the new class I created.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 Application.application、Application.root 或 event this.root (取决于您的需要)之类的东西,
但我不建议这样做(尝试重构您的代码,以便您可以传递对 main 或类似内容的引用...)。
问题是:您新创建的类是否负责将子级添加到组件中?那么我建议您可以将任何类型的 Container 的引用传递给它;在主 mxml 中,将引用传递给 root。
也可能新类只负责公开要添加的内容;在这种情况下,您可能可以将“this.addChild(...)”代码保留在主代码中。
希望这有帮助。
You could use something like Application.application, Application.root or event this.root (depending on your needs)
But I would'nt advise it (try to refactor your code so that you can pass a reference to your main or something like that ... ).
The question is : is your newly-created class going to be reponsible for adding childs to components ? Then I would suggest you instead make it possible to pass it a reference to any kind of Container ; and in you main mxml, you pass the reference to root.
It might also be that the new class is only responible for exposing the things to add ; in which case you could probably leave the "this.addChild(...)" code in the main.
Hoping this helps.