Silverlight與HTML雙向交互
摘要:HTML和Silverlight之間的雙向交互可以更靈活的使用Silverlight進行開發,上午摸索了一下,記錄在此。
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="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();
}
{
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);
}
{
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";
}
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之間雙向傳遞數據的功能。
全站熱搜