User Guide
Example: Using the External API with an ActiveX container 519
An ExternalInterfaceCall instance is a simple value object with two properties. The
FunctionName property contains the function name specified in the ActionScript
ExternalInterface.Call() statement. If any parameters are added in ActionScript, those
parameters are included in the ExternalInterfaceCall object’s
Arguments property. In this case,
the method that handles the event is simply a
switch statement that acts like a traffic
manager. The value of the
FunctionName property (e.FunctionCall.FunctionName)
determines which method of the AppForm class is called.
The branches of the
switch statement in the previous code listing demonstrate common
method call scenarios. For instance, any method must return a value to ActionScript (for
example, the
isReady() method call) or else should return null (as seen in the other method
calls). Accessing parameters passed from ActionScript is demonstrated in the
newMessage()
method call (which passes along a parameter
e.FunctionCall.Arguments[0], the first
element of the
Arguments array).
Calling an ActionScript function from C# using the ExternalInterfaceProxy class is even more
straightforward than receiving a function call from ActionScript. To call an ActionScript
function, you use the ExternalInterfaceProxy instance’s
Call() method, as follows:
/// <summary>
/// Called when the "Send" button is pressed; the value in the
/// MessageText text field is passed in as a parameter.
/// </summary>
/// <param name="message">The message to send.</param>
private void sendMessage(string message)
{
if (swfReady)
{
...
// Call the newMessage function in ActionScript.
proxy.Call("newMessage", message);
}
}
...
/// <summary>
/// Call the ActionScript function to get the current "availability"
/// status and write it into the text field.
/// </summary>
private void updateStatus()
{
Status.Text = (string)proxy.Call("getStatus");
}
...
}