Silverlight与javaScript相互交互

silverlight调用javaScript

javascript:

  function HelloWord(val) {
            alert("HelloWord:" + val);
        }

在silverlight中

    ScriptObject script = HtmlPage.Window.GetProperty("HelloWord") as ScriptObject;
            script.InvokeSelf("majiang");
          或者

HtmlPage.Window.Invoke("HelloWord", "majiang");

 

javascript调用Silverlight

一通过托管类

silverlight:

    [ScriptableType]
    public class ProductInfo
    {
        public string ShowProductPrice(string name, int price)
        {
            return string.Format("{0}:${1}", name, price);
        }
    }

注册类

  HtmlPage.RegisterCreateableType("product", typeof(ProductInfo));

客服端

    <div id="silverlightControlHost">
        <object id="sl" data="data:application/x-silverlight-2," type="application/x-silverlight-2"
            width="50%" height="50%">
            <param name="source" value="ClientBin/SilverlightApplication1.xap" />
            <param name="onError" value="onSilverlightError" />
            <param name="background" value="white" />
            <param name="minRuntimeVersion" value="4.0.50826.0" />
            <param name="autoUpgrade" value="true" />
            <a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=4.0.50826.0" style="text-decoration: none">
                <img src="http://go.microsoft.com/fwlink/?LinkId=161376" alt="Get Microsoft Silverlight"
                    style="border-style: none" />
            </a>
        </object>
        <iframe id="_sl_historyFrame" style="visibility: hidden; height: 0px; width: 0px;
            border: 0px"></iframe>
           <input type="button" value="Call Silvertlight" onclick="LoadProduct();" />
    </div>

 function LoadProduct() {
            var sl = document.getElementById("sl");
         var product = sl.Content.services.createObject("product");
                 var msg = product.ShowProductPrice('majiang', 123);
                    alert(msg);
        }

二通过托管成员

 public class CustomerInfo
    {
        [ScriptableMember]
        public string Name { get; set; }
        [ScriptableMember]
        public string Address { set; get; }
        [ScriptableMember]
        public string ShowCustomerInfo()
        {
            return string.Format("Name:{0}<br/>Address:{1}",Name,Address);
        }
    }

注册实例

 CustomerInfo customer = new CustomerInfo();
            HtmlPage.RegisterScriptableObject("customerInfo",customer);

客服端javaScrilpt

 function LoadProduct() {
            var sl = document.getElementById("sl");
            sl.Content.customerInfo.Name = "majiang";
            sl.Content.customerInfo.Address = "ffghhhf";
            var msg = sl.Content.customerInfo.ShowCustomerInfo();
            alert(msg);
        }

你可能感兴趣的:(JavaScript,iframe,String,Microsoft,Class,silverlight)