Well, I received a copy of the new Macromedia Flash 8 and had some fun playing around with some new features and for the sake of this blog, those features that concern interaction with .NET (sample code provided in VB.NET).
A couple of years ago, before the world had crushed my spirit, I wrote this little FAQ titled 'Communicating between Macromedia Flash and VB' @ VBcity and until Flash 7, those elements held true - apart from the mildly cool XML class in ActionScript 1.0 Library that came out with Flash 6 - but it was only file/web based access. (Would have been cool if it was Stream / byte based as well...)
So how does the Flash 8 control work differently? Well, the control still supports the 'setVariable' and 'FSCommand' events so the techniques described in the above article are fine. However, both methods are not ideal - especially in circumstances where you want to execute routines in either Flash or .NET without any convulted timer / flag based systems.
Settle down my pink-cheeked children and let me show you new jive that replaces the old shuffle.
Flash 8 now has a new class called 'flash.external.ExternalInterface' that allows the Flash movie to communicate to the actual Flash Container (or ActiveX container if you wish). Within this class are two methods, one for communicating to the control and one for adding functions that can be called from the control to the actual movie. These are...
- flash.external.ExternalInterface.addCallback - Which binds a function to the container allowing the container to call this function within the movie.
- flash.external.ExternalInterface.call - Which executes a function at the control and thus its parent container.
Another interesting element of this communication method is that data is now XML formatted. Now I haven't looked too much into the various syntax flags specific for Flash (maybe something for another day), but a string can be passed as an argument using <string>Hello World</string> (which also ties into the ActionScript 2.0 notation). So numerical values can be passed to and fro using <number>4</number>. In fact, arrays can also be passed, e.g. "<array><property id='0'><number>1</number></property><property id='1'><number>4</number></property><property id='2'><number>16</number></property></array>", which is cool rather than delimiting large amounts of data...
Anyway, you can download all the project files just here, and if you haven't got a copy of Flash 8 Authoring (to viwe the source FLA), you can still use the compiled Flash 8 SWF file - but you will need the Flash 8 player installed.
Have fun - M
Development Note: one interesting bug that occured was when I attempted to convert the e.request XML string from the FlashCall event into a .NET XML object. Originally I tried a Dataset, which kept throwing 'The path contains invalid characters' error. I tried an XMLTextReader that didn't work - I also tried converting the string using ASCII Encoder and UTF8 Encoder - but with no luck. In the end, I passed the XML string through a StringReader which seemed to do the trick! :-o