Silverlight與HTML雙向互動

來源:互聯網
上載者:User

Silverlight具備很好的使用者體驗,但有時需要在頁面的布局上進行特殊處理,比如作為webpart整合到Sharepoint中等等。

HTML和Silverlight之間的雙向互動可以更靈活的使用Silverlight進行開發,上午摸索了一下,記錄在此。

一,向Silverlight傳遞資料,實現個人化載入

Silverlight在HTML中的引用是:

<object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%"><param name="source" value="ClientBin/VideoCenter.xap"/><param name="onError" value="onSilverlightError" /><param name="background" value="white" /><param name="minRuntimeVersion" value="4.0.50401.0" /><param name="autoUpgrade" value="true" /><a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=4.0.50401.0" style="text-decoration:none"><img src="http://go.microsoft.com/fwlink/?LinkId=161376" alt="Get Microsoft Silverlight" style="border-style:none"/></a></object>這種引用外掛程式的方式提供了一系列的參數來實現個性載入,上面的代碼中我們加了一行參數
<param name="initParams" value="CategoryId=1" /> 
我們可以在Silverlight中處理這些參數,開啟Silverlight應用程式的App代碼檔案,加上接收參數的代碼
private void Application_Startup(object sender, StartupEventArgs e){if (e.InitParams.Count != 0){foreach(var item in e.InitParams){this.Resources.Add(item.Key, item.Value);}}this.RootVisual = new MainPage();}我們看到對e.InitParams的處理,即可得到了HTML中傳進來的參數,實現個人化載入:
if(App.Current.Resources["CategoryId"]!=null){int cateId = int.Parse(App.Current.Resources["CategoryId"].ToString());CategoryItem c = new CategoryItem();c.CategoryID = cateId;this.gridOfList.Children.Add(c);}
二,Silverlight調用HTML中的指令碼資源,實現自身的樣式等修改
首先我們可以在載入Silverlight組件的頁面上編寫一段Javascript指令碼
function InvokePlayer(videoId) {document.getElementById("divCategory").style.display = "none";var player = document.getElementById("divPlayer");player.style.width = "100%";player.style.height = "600px";}
怎麼在Silverlight中調用這個指令碼呢?我們可以在某個事件中調用Silverlight提供的類方法
System.Windows.Browser.HtmlPage.Window.Invoke("InvokePlayer", videoId);
當然類似的方法還有幾個:
(HtmlPage.Window.GetProperty("InvokePlayer") as ScriptObject)InvokeSelf("Good Function!");
HtmlPage.Window.Eval("document.getElementById('result')") as HtmlElement…即可實現呼叫指令碼。

經過以上的兩個方法即可實現HTML和Silverlight之間雙向傳遞資料的功能。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.