锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
As I said before, communication between Flash Player and its container has been made extremely easy. The new class that makes this process so easy is the ExternalInterface. We will begin in the ActionScript. First, we need to import this new class so we can use it (as2 only, in as3 it will work without the import):
import flash.external.ExternalInterface;
Next, we have to register any function we want to make available externally:
ExternalInterface.addCallback("addText", addText);
Basically, the code above will allow us to call the addText function (which I will show in a minute) from the C# application.
The addText function is as below. Basically, it takes a string input and appends it to a text box
function addText(val:String):void { inTxt.appendText(val + "\n"); // append text recieved from c# }
That’s it from the ActionScript side. Now all we need to do is call the function from C#. First, I add an instance of the Flash Player ActiveX control to my form and load the SWF we created in the form’s constructor:
private AxShockwaveFlash player; public DemoForm () { ... player.LoadMovie(0, Application.StartupPath + "\\EITest.swf"); player.Play(); ... }
Next, all we have to do is call the externalized method when desired. In my case, it is in response to the user clicking the send button:
private void sendBtn_Click(object sender, EventArgs e) { player.CallFunction("" + outTxt.Text + ""); }
Again, you will need to use the ExternalInterface in the ActionScript:
function send(evt : Event):void { ExternalInterface.call("sendText", outTxt.text); // function to call and it's parameters outTxt.text = ""; // reset text box }
As you can see, I am calling a method sendText and passing the input string as a parameter. Now to receive the message in C#, we first have to subscribe to the FlashCall event. You can do this in the constructor or from the activex properties panel on events tab.
Now the call made in ActionScript will be received in the request property of the event argument. For my particular call, the XML will look like this:
<invoke name="sendText" returntype="xml"> <arguments> <string>some text message here</string> </arguments> </invoke>
So now all we have to do is parse the XML in the event handler and invoke the C# function locally:
private void player_FlashCall(object sender, _IShockwaveFlashEvents_FlashCallEvent e) { // message is in xml format so we need to parse it XmlDocument document = new XmlDocument(); document.LoadXml(e.request); // get attributes to see which command flash is trying to call XmlAttributeCollection attributes = document.FirstChild.Attributes; String command = attributes.Item(0).InnerText; // get parameters XmlNodeList list = document.GetElementsByTagName("arguments"); // Interpret command switch (command) { case "sendText" : resultTxt.Text = list[0].InnerText; break; case "Some_Other_Command" : break; } }
You use this syntax to embed an asset in an ActionScript file, or in an <mx:Script> block in an MXML file. For more information, see Using the [Embed] metadata tag.
You use this syntax in an MXML tag definition to embed an asset. For more information, see Using the @Embed() directive in MXML.
You use this syntax in an <mx:Style> block in an MXML file to embed an asset. For more information, see Embedding assets in style sheets.
All three varieties of the embed syntax let you access the same assets; the only difference is where you use them in your application.
You can use the slash character (\) to escape the at sign character (@) when you want to use a literal @ character. For example, the string "\@Embed(foo)" means the literal string "@Embed(foo)"). You use two slash characters (\\) to escape a single backslash character. For example, use the character string "\\@" to specify the literal strings "\@".
Each form of the embed syntax takes one or more optional parameters. The exact syntax that you use to embed assets depends on where they are embedded. Some of these parameters are available regardless of what type of asset you are embedding, and others are specific to a particular type of media. For example, you can use the source and mimeType parameters with any type of media, but the scaleGridRight parameter applies only to images.
The following table describes the parameters that are available for any type of embedded asset. For more information, see About the source parameter and About the MIME type.
Parameter |
Description |
---|---|
source |
Specifies the name and path of the asset to embed; either an absolute path or a path relative to the file containing the embed statement. The embedded asset must be a locally stored asset. Therefore you cannot specify a URL for an asset to embed. For more information on setting the path, see About setting the path to the embedded asset. |
mimeType |
Specifies the mime type of the asset. |
The following table describes the parameters that are specific for images and Sprite objects. For more information, see Using 9-slice scaling with embedded images.
Parameter |
Description |
---|---|
scaleGridTop |
Specifies the distance in pixels of the upper dividing line from the top of the image in a 9-slice scaling formatting system. The distance is relative to the original, unscaled size of the image. |
scaleGridBottom |
Specifies the distance in pixels of the lower dividing line from the top of the image in a 9-slice scaling formatting system. The distance is relative to the original, unscaled size of the image. |
scaleGridLeft |
Specifies the distance in pixels of the left dividing line from the left side of the image in a 9-slice scaling formatting system. The distance is relative to the original, unscaled size of the image. |
scaleGridRight |
Specifies the distance in pixels of the right dividing line from the left side of the image in a 9-slice scaling formatting system. The distance is relative to the original, unscaled size of the image. |
The following table describes the parameter that is specific to SWF files. For more information, see Embedding SWF files.
Parameter |
Description |
---|---|
symbol |
Specifies the symbol in a SWF file to embed, for use with Adobe Flash Player 8 and earlier. |
In almost all cases, you must specify the source parameter, or nothing is embedded.
The source parameter is the default parameter of the [Embed] metadata tag; therefore, if you are not specifying any other parameters, you can just supply its value without explicitly including the parameter name or assigning it the desired value, as the following example shows:
You can specify a fully qualified path to the image or a URL, as the following examples show:
If the path does not start with a slash character, Flex first searches for the file relative to the file that contains the [Embed] metadata tag. For example, the MXML file testEmbed.mxml includes the following code:
<mx:Button label="Icon Button" icon="@Embed(source='assets/logo.gif')"/>
In this example, Flex searches the subdirectory named assets in the directory that contains the testEmbed.mxml file. If the image is not found, Flex then searches for the image in the SWC files associated with the application.
If the path starts with a slash character, Flex first searches the directory of the MXML file for the asset, and then it searches the source path. You specify the source path to the Flex compiler by using the source-path compiler option. For example, you set the source-path option as the following code shows:
-source-path=a1,a2,a3
The MXML file a1/testEmbed.mxml then uses the following code:
<mx:Button label="Icon Button" icon="@Embed(source='/assets/logo.gif')"/>
Flex first searches for the file in a1/assets, then in a2/assets, and then in a3/assets. If the image is not found, Flex searches for the image in the SWC files associated with the application.
If the MXML file is in the a2 directory, as in a2/testEmbed.mxml, Flex first searches the a2 directory and then the directories specified by the source-path option.
You can optionally specify a MIME type for the imported asset by using the mimeType parameter. If you do not specify a mimeType parameter, Flex makes a best guess about the type of the imported file based on the file extension. If you do specify it, the mimeType parameter overrides the default guess of the asset type.
Flex supports the following MIME types.
You can use the [Embed] metadata tag to import JPEG, GIF, PNG, SVG, SWF, TTF, and MP3 files.
You must use the [Embed] metadata tag before a variable definition, where the variable is of type Class. The following example loads an image file, assigns it to the imgCls variable, and then uses that variable to set the value of the source property of an Image control:
The syntax for the dispatchEvent() method is as follows:
objectInstance.dispatchEvent(event:Event):Boolean
When dispatching an event, you must create a new Event object. The syntax for the Event object constructor is as follows:
Event(event_type:String, bubbles:Boolean, cancelable:Boolean)
The event_type parameter is the type property of the Event object. The bubbles and cancelable parameters are optional and both default to false. For information on bubbling and capturing, see Event propagation.
You can use the dispatchEvent() method to dispatch any event you want, not just a custom event. You can dispatch a Button control's click event, even though the user did not click a Button control, as in the following example:
You can also manually dispatch an event in an MXML tag. In the following example, moving the mouse pointer over the button triggers the button's click event:
杞澆:http://livedocs.adobe.com/flex/3/html/events_07.html#220526
杞澆:http://www.uncool.cn/blogs/read.php?238