C# flash想到调用函数解决办法(二)参数传递

如果该事件处理函数的 EventArgs 为 e, 则有如下对应关系:

e.command -> flash 中调用 FSCommand 时的 command 参数;
e.args -> flash 中调用 FSCommand 时的 arguments 参数。

在 C# 中往 Flash 传递数值用 SetVariable 方法:

axShockwaveFlash1.SetVariable("variablename""valueasstring");

对应的,有一个 GetVariable 方法可以获得 flash 中的变量值。

FSCommand 的参数只能传递一个,所以其功能存在一定的局限性。在你需要传递多个参数的时候,则需要在调用方用字符串拼接各个参数值,然后在被调用方重新解开。这种办法使得复杂的传值显得很丑陋。

不过,除了 FSCommand 外,我们还有另一种办法和 flash 通信,就是使用 Flash Player 8 External API.
(http://www.codeproject.com/cs/media/flashexternalapi.asp)

使用 External API,可以实现 ActionScript 和 C# 的双向调用。其好处就在于每次调用时的参数和返回值都通过一个 xml 文档来传递,这样就比 FSCommand 功能完善了很多。当然在 C# 中,我们也需要声明一下外部函数实现的对应关系。

代码片段:
Flash ActionScript 中声明提供给 hosting application 调用的回调函数:

import flash.external.ExternalInterface;
ExternalInterface.addCallback(
"loadAndPlayVideo"null, loadAndPlayVideo);

C# 中调用 Flash 的情形:

     flashPlayer.CallFunction("<invoke" + 
          
" name=\"loadAndPlayVideo\" returntype=\"xml\">
          <arguments><string>" + fileDialog.FileName + 
          "</string></arguments></invoke>"); 

在 ActionScript 中调用 C# 函数:

ExternalInterface.call("ResizePlayer"
      videoPlayer.metadata.width, videoPlayer.metadata.height);

在 C# 中声明被 Flash 调用的函数:

flashPlayer.FlashCall += 
  
new _IShockwaveFlashEvents_FlashCallEventHandler(flashPlayer_FlashCall);

在此,我们可以看到 C# 和 Flash 进行通信还是相当方便的。

除此之外,我顺便搜索到其他一些和 flash 有关的不错的文章:

Flash and .NET with FlashRemoting
http://www.codeproject.com/aspnet/FlashRemoting.asp

Multiple File Upload With Progress Bar Using Flash and ASP.NET
http://www.codeproject.com/aspnet/FlashUpload.asp

Flash GUI for your exe using minimalistic approach (C++)
http://www.codeproject.com/useritems/FlashGui.asp

posted @ 2008-10-22 09:45  Wind&#183;e  阅读(2171)  评论(0编辑  收藏  举报