davin

Just a little thinking and interest!

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

在silverlight中调用外部程序的几种方法总结如下:

1.silverlight不支持oob模式的时候,silverlight调外部应用程序只能通过activex来实现。

   大致方法如下,之前写的一个:

       var idropItems;
        var clsid;
        var plugin;
        var currentDivIndex;
        jQuery(document).ready(function() {
            idropItems = window.parent.GetIDropItems();
            if (idropItems.length > 0) {

                BuildDivContainer();

                //activex的clsid,

                clsid = "clsid:21E0CB95-1198-4945-A3D2-4BF804295F78";
                $("#Pagination").pagination(idropItems.length, {
                    num_edge_entries: 2,
                    num_display_entries: 8,
                    items_per_page: 8,
                    next_text: "",
                    prev_text: "",
                    callback: pageselectCallback
                });
            }
        });
        function pageselectCallback(page_id, jq) {
            BuildIDropItems(page_id);
        }

   function BuildIDropItems(pageIndex) {

            $("#c_" + currentDivIndex).hide();
            if ($("#c_" + pageIndex).attr("loaded") == "0") {
                var itemIndex = pageIndex * 8;
                var html = "";
                for (var i = itemIndex; i < itemIndex + 8; i++) {
                    if (i < idropItems.length) {
                        if (idropItems[i].ObjectType == '2') {
                            var objectName = GetSubstring(idropItems[i].ObjectName.substring(0, idropItems[i].ObjectName.lastIndexOf('.')), 13);
                            var ext = idropItems[i].ObjectName.substring(idropItems[i].ObjectName.lastIndexOf('.') + 1).toLowerCase();
                                                         html += "<div class='ItemBox'>";
                                html += "<object classid='" + clsid + "' width='101' height='101' >";
                                html += "<param name='background' value=''>";
                                html += "<param name='proxyrect' value='0, 0, 101, 101'>";
                                html += "<param name='griprect' value='0, 0, 101, 101'>";
                                html += "<param name='package'  value='GetIDropItem.aspx?guid=" + idropItems[i].ObjectId + "&type=xml'>";
                                html += "<param name='validate' value='1'>";
                                html += "<img src='GetIDropItemguid=" + idropItems[i].ObjectId + ".img' title= />";
                                html += "</object>";
                                html += "<span class='ItemName'>" + objectName + "</span>";
                                html += "</div>";                                      

                        }
                    }
                }
                $("#c_" + pageIndex).html(html).attr("loaded", "1");
            }
            $("#c_" + pageIndex).show();
            currentDivIndex = pageIndex;
        }

2.在silverlight4里面,可以在oob模式下可以直接调com组件,如下面代码段:就是通过AutomationFactory调用dt930的com组件
      if (Application.Current.InstallState != InstallState.Installed)
          Application.Current.Install();

      //oob模式,需要提升权限
      if (Application.Current.HasElevatedPermissions && System.Windows.Interop.ComAutomationFactory.IsAvailable)
       {

          ///AutomationFactory call com componnnet          
            dynamic dt = AutomationFactory.CreateObject("DT390COM.DT390");

      }
需引用下面的命名空间

using System.Dynamic;

using System.Windows.Interop;
using System.Runtime.InteropServices.Automation;

3.上面2种方法都要求调用的外部程序是com组件,如果不是com组件,又该如何去启动外部程序呢.通过使用WScript.Shell 组件可以打开任何的应用程序

  eg  using (dynamic shell = AutomationFactory.CreateObject("WScript.Shell"))
         {
                shell.Run(@"C:\windows\notepad.exe"); //you can open anything
               shell.SendKeys(txtTextToSend.Text);
               
          }

  除此之外还可以在js中调用

      <javascript language="javascipt">{

       var shell = new ActiveXObject("WScript.shell");

       shell.Run(@"C:\windows\notepad.exe");

    }

posted on 2010-08-15 11:36  davin  阅读(2235)  评论(2编辑  收藏  举报