access mxml component from external actionscript file

老子叫甜甜 提交于 2019-12-13 18:47:23

问题


i'm trying to access an mxml component from my external as file. e.g

main.mxml:<br>
<code>[mx:text id="myText" />]</code>

file.as:<br>
<code>var mainM:main = new main();

mainM.text.visible = true;</code>

I get the following error:

[TypeError: Error #1009: Cannot access a property or method of a null object reference]

Any suggestions on how to approach it better.


回答1:


The ID of your component instance becomes a member of your application and can easy be accessed like so

import mx.core.Application;
mx.core.Application.application.myText.visible = true;



回答2:


An additional answer is that when you create a new Flex component (new myFlexComponent()), the child UI components are not created until a CREATION_COMPLETE call is invoked, indicating the component is fully created. In the case of application, there is only one, and its automatically created by the framework, and referenced by (Application.application) as stated above.

For example, if your variable was a simple class variable (e.g. myDate:Date), you could access it via the above syntax



来源:https://stackoverflow.com/questions/2289314/access-mxml-component-from-external-actionscript-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!