Hi all…...
I’m having a problem that has been really getting to me for the last 12 hours or so and I can’t seem to find the answer anywhere. I know it’s something simple, but I just don’t know the correct syntax.
I’ve got a Flex Application in Flash Builder 4 with a file called “Test.mxml” which looks like this (simplified):
<?xml version="1.0" encoding="utf-8"?>
<mx:application minwidth="955" backgroundcolor="#E8F7FF" layout="absolute" initialize="init();" color="#C0E7EF" height="386" minheight="600" xmlns:mx="http://www.adobe.com/2006/mxml" width="485">
<mx:script source="Master.as"> </mx:script>
// there are some buttons and stuff here.
<mx:text fontfamily="Verdana" x="28.5" y="324" id="myText" color="#E8DA02" textalign="center" fontsize="24" fontweight="bold" width="420" />
</mx:application>
After that, I have a file called “Master.as” which is referenced as the source in Test.mxml. It is not a class or anything…..just has several functions in it.
It looks something like this (simplified, of course):
// imports and variables up here
private var c1:class1 = new class1;
private function init():void {
c1.doStuff();
}
public function Write():void {
// writes some text to a text area in the mxml file.
myText.text = "Hello World";
}
Class1 looks like this…..
package Methods {
// imports here
import mx.core.Application;
public class Flash_Based extends Application {
// vars here
private var app:Test = new Test; // Test.mxml
public function class1():void {
// does stuff.
}
private function callBack():void {
app.Write();
}
}
}
Now, what I’m having a hard time with is app.Write(); in class1. I need to be able to access elements and trigger functions in Master.as (which, remember, is the source of Test.mxml).
Basically, all I’m trying to do is reference functions and elements in the MXML document source from other classes. I know the MXML document is basically a class so I figure you just reference it like anything else ( private var app:Test = new Test; ) but I guess not.
Does this make sense? It does not work when I try to do this….....in other words, the text “Hello World” is not written to the text area.
It should get to class1’s function callBack() and trigger the Write(); function in Master.as.
Any suggestions? Should I provide more information?
Sorry for being so long winded. 
Thanks for your help!
Alex
try using Application.application instead of referencing with Class name directly.
http://livedocs.adobe.com/flex/3/html/help.html?content=app_container_3.html
Hi jalexsmith,
Adobe recommends that you use the spark.components.Application class as an alternative to mx.core.Application
Try with this:
import mx.core.FlexGlobals; ..... FlexGlobals.topLevelApplication.Write();
Alex,
private var app:Test = new Test(); is not a reference to your existing app but creation of new instance of your Test class.
Try what sergibh recommended.
sergibh said
Hi jalexsmith,Adobe recommends that you use the spark.components.Application class as an alternative to mx.core.Application
Try with this:
import mx.core.FlexGlobals; ..... FlexGlobals.topLevelApplication.Write();
Yep FlexGlobals.topLevelApplication is the new Application.application for Flex 4
I think in order to use FlexGlobals,You will have to change the mx:application tags to s:application
And you will wanna use fx:script instead of mx:script.
There maybe be another couple things you run into as well.
codempire said
I think in order to use FlexGlobals,You will have to change the mx:application tags to s:applicationAnd you will wanna use fx:script instead of mx:script.
There maybe be another couple things you run into as well.
I think is better to change to s:application but it is not necessary
TestApp.mxml:
<?xml version="1.0" encoding="utf-8"?>
<mx:application minwidth="955" layout="absolute" xmlns:local="*" minheight="600" xmlns:mx="http://www.adobe.com/2006/mxml">
<local:component />
<mx:button label="Label" id="test" />
</mx:application>
component.mxml:
<?xml version="1.0" encoding="utf-8"?>
<mx:container creationcomplete="init()" height="50" xmlns:mx="http://www.adobe.com/2006/mxml" width="100">
<mx:script>
<![CDATA[
import mx.core.FlexGlobals;
public function init():void {
FlexGlobals.topLevelApplication.prueba.test = "LABEL";
}
]]>
</mx:script>
</mx:container>
Thanks for all of the replies guys! I really appreciate it and you solved my issue! I knew it was small – I just didn’t know what it was and didn’t know exactly what to search for.
Sergibh was the one who got it first…...it was just usingimport mx.core.FlexGlobals; ..... FlexGlobals.topLevelApplication.Write();
That solved my problem and no further changes or modifications were necessary like s:Application or anything.
Thanks again!
