Ian,
Excellent explanation. Knowing that mxml files are really just class
files...well, it's like turning on a light. Thank you!
Incidentally, it turns out that
Application.application.myChart.graphics...etc. works fine as well. I
was copying the use of "Application(Application.application)" from the
Moock book, but in that case the author was *adding* components to the
new Application object. That makes sense, since Application always
lets one add children, but the compiler only "knows" that
"Application(Application.application)" is an Application, not whether
or not it possesses an instance variable called myChart.
Good day,
Steve
On Tue, Sep 30, 2008 at 7:30 PM, Ian Thomas <[EMAIL PROTECTED]> wrote:
> Hi Steve,
>
> It's because the MXML file myApp.mxml is equivalent to a class file.
>
> So what you're really saying is:
>
> myApp.as: (roughly translated from myApp.mxml, omitting imports)
> package
> {
> public class myApp extends Application
> {
> public var myChart:VBox;
>
> public function myApp()
> {
> addEventListener(FlexEvent.APPLICATION_COMPLETE,onApplicationComplete);
> }
>
> override protected function createChildren():void
> {
> super.createChildren();
> var panel:Panel=new Panel();
> addChild(panel);
> myChart=new VBox();
> panel.addChild(myChart);
> }
>
> private function onApplicationComplete(ev:FlexEvent):void
> {
> EntryClass.main();
> }
> }
>
> (incidentally, you can now see it's a class - it would have been
> better to name it MyApp.mxml)
>
> And, from your own code for EntryClass.as:
> package {
> public class EntryClass {
> public static function main():void {
> myChart.graphics.drawRect(100,100,100,100);
> }
> }
> }
>
>
> I hope you can see that when EntryClass.main() gets called, EntryClass
> has no reference to any property or function called 'myChart', because
> myChart is defined by (and contained in) myApp.as (which is
> automagically created from your own myApp.mxml file).
>
> You need to get at the myApp instance from within main(). So your idea
> about using Application.application is correct, just you weren't quite
> using it right.
>
> Your code was:
>
> var mxmlApp:Application = Application(Application.application);
> mxmlApp.myChart.graphics.drawRect(100,100,100,100);
>
> The trouble with this is that you are casting Application.application
> to the class Application. The Application class doesn't define any
> variable called myChart, so the compiler throws an error. However, the
> myApp class _does_, and you know that Application.application is
> actually returning a myApp. So the correct thing to do is to cast it
> to myApp, like so:
>
> var mxmlApp:myApp = myApp(Application.application);
> mxmlApp.myChart.graphics.drawRect(100,100,100,100);
>
> although actually in AS3 I prefer this syntax, because I think it's clearer:
>
> var mxmlApp:myApp = Application.application as myApp;
> mxmlApp.myChart.graphics.drawRect(100,100,100,100);
>
>
> Things to take away from this:
>
> - MXML files are just classes. That's all the Flex compiler does when
> it compiles an mxml file - turns it into an .as file, then compiles.
> You can actually set a compiler flag to look at the .as files that are
> generated.
> - You need to cast to the right class i.e. the class that defines the
> property or method you're trying to get at.
> - You can't 'magically' refer to properties (such as myChart) without
> having some reference to the thing that defines the property.
> - You should name your .mxml file starting with an uppercase, as it's
> really a class - it should be MyApp.
>
>
> I hope that helps, and is clear!
>
> Cheers,
> Ian
>
>
> On Tue, Sep 30, 2008 at 7:09 PM, steve linabery <[EMAIL PROTECTED]> wrote:
>> Hello Flashcoders,
>>
>> Let's say I have two files, myApp.mxml:
>> <?xml version="1.0"?>
>> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
>> applicationComplete="EntryClass.main()">
>> <mx:Panel>
>> <mx:VBox id="myChart" width="900" height="300" />
>> </mx:Panel>
>> </mx:Application>
>>
>> and EntryClass.as:
>> package {
>> public class EntryClass {
>> public static function main():void {
>> myChart.graphics.drawRect(100,100,100,100);
>> }
>> }
>> }
>>
>>
>> Why does "mxmlc myApp.mxml" complain with this error message:
>>
>> quote:
>> /path/to/EntryClass.as(4): col: 7 Error: Access of undefined
>> property myChart.
>>
>> myChart.graphics.drawRect(100,100,100,100);
>> ^
>>
>> According to devguide_flex3.pdf:
>> "The IDs for all tags in an MXML component, no matter how deeply
>> nested they are, generate public variables of the component being
>> defined. As a result, all id properties must be unique within a
>> document. This also means that if you specified an ID for a component
>> instance, you can access that component from anywhere in the
>> application: from functions, external class files, imported
>> ActionScript files, or inline scripts."
>>
>> I thought perhaps this would work:
>> package {
>> public class EntryClass {
>> public static function main():void {
>> var mxmlApp:Application = Application(Application.application);
>> mxmlApp.myChart.graphics.drawRect(100,100,100,100);
>> }
>> }
>> }
>>
>> but mxmlc complains about this with:
>> /path/to/EntryClass.as(7): col: 15 Error: Access of possibly undefined
>> property myChart through a reference with static type
>> mx.core:Application.
>>
>> Any suggestions appreciated!
>>
>> Thanks,
>> Steve
>> _______________________________________________
>> Flashcoders mailing list
>> [email protected]
>> http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
>>
> _______________________________________________
> Flashcoders mailing list
> [email protected]
> http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
>
_______________________________________________
Flashcoders mailing list
[email protected]
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders