<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    沙漠中的魚

    欲上天堂,先下地獄
    posts - 0, comments - 56, trackbacks - 0, articles - 119
      BlogJava :: 首頁 ::  :: 聯(lián)系 :: 聚合  :: 管理

    Flex與C#交互(swf)

    Posted on 2009-11-26 16:51 沙漠中的魚 閱讀(2237) 評論(1)  編輯  收藏 所屬分類: 其他Flex
    The External API allows an ActionScript developer to easily interact with the container program that is hosting Flash Player 8 and vice versa. The majority of the time, this will most likely be a Web browser, but this does not always have to be the case.

    As many C# developers know, it is easy to house an ActiveX control (the IE version of Flash Player) in a .NET Windows application. This means we can now load an SWF in our Windows application and easily send data back and forth. Keep in mind that the keyword in this statement is “easily;” although possible before, it was not nearly as simple as the External API makes it now!

    C# to ActionScript Communication

    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 + "");
    }

    ActionScript to C# Communication

    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;
    }
    }

    附件http://www.tkk7.com/Files/aoneany/CFlashProject.zip

    評論

    # re: Flex與C#交互(swf)  回復(fù)  更多評論   

    2010-06-18 16:10 by 路過
    我看了 你的代碼 說明 受益匪淺。。。

    swf 與 c#交互 必須用 xml嗎?

    主站蜘蛛池模板: 精品韩国亚洲av无码不卡区| 亚洲第一精品在线视频| 亚洲国产成人精品无码一区二区 | 99热在线观看免费| av在线亚洲欧洲日产一区二区| 国产精品亚洲精品观看不卡| 日本免费大黄在线观看| 亚洲bt加勒比一区二区| 免费看黄的成人APP| 成人午夜亚洲精品无码网站| 国产A∨免费精品视频| 国产精品xxxx国产喷水亚洲国产精品无码久久一区 | 好爽好紧好大的免费视频国产| 亚洲嫩草影院在线观看| 97在线视频免费播放| 亚洲熟妇av一区| 四虎成人免费大片在线| 69视频免费在线观看| 亚洲日本在线播放| 好男人www免费高清视频在线| 亚洲一区二区三区免费在线观看| 亚洲午夜久久久影院| 午夜爽爽爽男女免费观看影院| 亚洲AV中文无码乱人伦下载| 91精品免费观看| 18禁在线无遮挡免费观看网站| 亚洲人成电影福利在线播放| 国产精品久久免费| 台湾一级毛片永久免费| 亚洲看片无码在线视频| 免费欧洲毛片A级视频无风险| 成人无码WWW免费视频| 精品无码国产污污污免费网站国产| 久久久青草青青亚洲国产免观 | 青青青国产在线观看免费| 猫咪免费人成网站在线观看入口| 亚洲AV永久无码精品一百度影院 | 亚洲AV网站在线观看| 131美女爱做免费毛片| 人人鲁免费播放视频人人香蕉| 久久亚洲精品无码VA大香大香|