access mxml component from external actionscript f

2019-08-14 19:57发布

问题:

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