This one came as a bit of an unwelcome surprise, in a kind of “didn’t work as I expected” kind of way.
Consider, say, a config class that needs to prompt the user for two files to use (in the case where I came across this, a database file and a directory of images, for instance) .
I’ve found (at least with flexbuilder beta3 and windows XP), that you cannot chain one file browse operation from the select event of another. (BrowseForOpen or browseForFolder are the only ones I’ve tested so far) .
The example mxml text below should demonstrate this …
The trick to fix it is to call the second browse operation via callLater() (if your class extends UIComponent) or a timer (if not).
so to fix the below you would change “checkConfig() ;” in the function file1Selected() to : this.callLater(checkConfig);
I also noticed that in that file the first file browse dialogue appears *beneath* the main application widow, which is a bit rubbish really.
That does make sense, in that the application window may not be rendered to screen in AIR on creationComplete, therefore the browse widow is placed on screen before the window of the AIR application (which appears above it since it was rendered to screen later on).
So … Change the call to checkConfig to the “windowComplete” event of WindowedApplication.
(I first tried the “applicationActivate” event, but seems that gets called whenever the AIR window is focused (or something), so caused some chaos and , confusingly, the same error. Because, of course, checkConfig() was called whenever the file browser was closed and the AIR window activated. DOH!)
As I said, this didn’t work as I’d expected. Is it *wrong*? I don’t know. I’d prefer it didn’t work this way, but so long as I know it does, I can work with it.
Which is really the point of writing about it, hope it helps anyone else lost on the same thing.
View the final, error free mxml file here.
=> this is the original example (that didn’t work…)
<mx:WindowedApplication xmlns:mx=”http://www.adobe.com/2006/mxml” layout=”absolute” width=”674″ height=”343″ creationComplete=”checkConfig()”>
<mx:Script>
<![CDATA[
[Bindable]
private var filePath1:String;
[Bindable]
private var filePath2:String;
private function checkConfig():void
{
if(filePath1 == null)
{
var file1:File = new File();
file1.addEventListener(Event.SELECT,file1Selected);
file1.browseForOpen(”pick a file, any file”);
return;
}
if(filePath2 == null)
{
var file2:File = new File();
file2.addEventListener(Event.SELECT,file2Selected);
file2.browseForOpen(”pick a file, any file”);
return;
}
}
private function file1Selected(event:Event):void
{
var f:File = event.target as File;
f.removeEventListener(Event.SELECT,file1Selected)
filePath1 = f.nativePath;
checkConfig();
}
private function file2Selected(event:Event):void
{
var f:File = event.target as File;
f.removeEventListener(Event.SELECT,file2Selected)
filePath2 = f.nativePath;
}
]]>
</mx:Script>
<mx:Text x=”142″ y=”33″ width=”492″ id=”txtFilePath1″ text=”{filePath1}”/>
<mx:Text x=”142″ y=”83″ width=”492″ id=”txtFilePath2″ text=”{filePath2}”/>
<mx:Label x=”10″ y=”83″ text=”config file 2″/>
<mx:Label x=”10″ y=”33″ text=”config file 1″/>
</mx:WindowedApplication>