NativeWindow class in AIR does not support Flex components

Just a quick tip for any AIR developer’s searching for why there’s no addChild method in the NativeWindow class – I wasted a fair while on this over the weekend. I was adding Flex components to the stage property of NativeWindow and trying various other work arounds. Although you don’t get errors for doing this the Flex components never appear when you test your app!

If you want to add Flex components to a new NativeWindow in AIR then this is what Window class is for. So don’t waste time creating a new NativeWindow and activating it, just create/subclass the Window class not NativeWindow and you’ll find all the stanard Flex component properties and methods 🙂


Auto centre main AIR Application window with code

Here’s a tip for how to centre (center for you yanks) your main Adobe AIR application window at runtime. It’s not as obvious as this.x = numX, this.y = numY for some reason. All this does is ends up moving you application container out of the NativeWindow masked view. Not what I was after.

You can set the initial position of your main application window in your app descriptor xml (although I can’t see the point of this as most developers are going to want it set dynamically based on the dimensions of the user’s screen).

So here’s the code that get’s called once the windowComplete handler is called:

private function centreApp():void
var screenBounds:Rectangle = Screen.mainScreen.visibleBounds;
var w:int = width;
var h:int = height;

var x:int = screenBounds.x + ((screenBounds.width-w)/2);
var y:int = screenBounds.y + ((screenBounds.height-h)/2);
stage.nativeWindow.x = x;
stage.nativeWindow.y = y;
visible = true;

In the main application I’m building I’m using the PureMVC Flex Framework and I’m not actually running this code snippet until my config xml and my styles swf has completed load into the main application so my code retains full control of when the application displays and where it displays 🙂

Download the App

Download the Source