添加Silverlight應用到HTML

來源:互聯網
上載者:User

標籤:style   blog   http   java   color   使用   

Silverlight是跨瀏覽器,跨客戶平台的瀏覽器外掛程式,可以應用在Windows,Linux,Mac等平台。作為瀏覽器外掛程式,Silverlight可以像Flash一樣,很方便的嵌套在HTML頁面中,下面我來介紹一下,如何添加Silverlight應用到HTML頁面。

 1. 首先,我們來看看VS2008自動產生的程式碼,建立一個Silverlight應用項目,Html_SilverlightChina,建立的時候選中將Silverlight項目分布到Web項目中。 2. 建立項目後,在Web項目中,會自動產生測試文檔頁面,分別是Html_SilverlightChinaTestPage.aspx和Html_SilverlightChinaTestPage.html,由於本文是講述如何添加Silverlight應用到HTML,所以,我們將集中講述Html_SilverlightChinaTestPage.html頁面代碼。   HTML代碼 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 2 <html xmlns="http://www.w3.org/1999/xhtml" >
 3 
 4 <head>
 5     <title>Html_SilverlightChina</title>
 6     <style type="text/css">
 7     html, body {
 8         height: 100%;
 9         overflow: auto;
10     }
11     body {
12         padding: 0;
13         margin: 0;
14     }
15     #silverlightControlHost {
16         height: 100%;
17         text-align:center;
18     }
19     </style>
20     <script type="text/javascript" src="Silverlight.js"></script>
21     <script type="text/javascript">
22         function onSilverlightError(sender, args) {
23             var appSource = "";
24             if (sender != null && sender != 0) {
25               appSource = sender.getHost().Source;
26             }
27             
28             var errorType = args.ErrorType;
29             var iErrorCode = args.ErrorCode;
30 
31             if (errorType == "ImageError" || errorType == "MediaError") {
32               return;
33             }
34 
35             var errMsg = "Unhandled Error in Silverlight Application " +  appSource + "\n" ;
36 
37             errMsg += "Code: "+ iErrorCode + "    \n";
38             errMsg += "Category: " + errorType + "       \n";
39             errMsg += "Message: " + args.ErrorMessage + "     \n";
40 
41             if (errorType == "ParserError") {
42                 errMsg += "File: " + args.xamlFile + "     \n";
43                 errMsg += "Line: " + args.lineNumber + "     \n";
44                 errMsg += "Position: " + args.charPosition + "     \n";
45             }
46             else if (errorType == "RuntimeError") {           
47                 if (args.lineNumber != 0) {
48                     errMsg += "Line: " + args.lineNumber + "     \n";
49                     errMsg += "Position: " +  args.charPosition + "     \n";
50                 }
51                 errMsg += "MethodName: " + args.methodName + "     \n";
52             }
53 
54             throw new Error(errMsg);
55         }
56     </script>
57 </head>
58 <body>
59     <form id="form1" runat="server" style="height:100%">
60     <div id="silverlightControlHost">
61         <object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%">
62           <param name="source" value="ClientBin/Html_SilverlightChina.xap"/>
63           <param name="onError" value="onSilverlightError" />
64           <param name="background" value="white" />
65           <param name="minRuntimeVersion" value="3.0.40818.0" />
66           <param name="autoUpgrade" value="true" />
67           <a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=3.0.40818.0" style="text-decoration:none">
68                <img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style:none"/>
69           </a>
70         </object><iframe id="_sl_historyFrame" style="visibility:hidden;height:0px;width:0px;border:0px"></iframe></div>
71     </form>
72 </body>
73 </html>  3. 在上面的HTML代碼中,主要代碼分為三部分,第一部分是樣式表CSS代碼,第二部分是Javascript指令碼代碼,第三部分是HTML Body代碼。對於CSS樣式代碼,這裡不再贅述。我們來分析一下Javascript代碼和HTML Body代碼。首先說說Javascript指令碼代碼,在Javascript指令碼中,有一個onSilverlightError函數,該函數的功能是處理Silverlight應用外部例外狀況錯誤資訊的。例如,如果瀏覽器無法下載XAP檔案,該錯誤資訊會由onSilverlightError函數處理,將會在瀏覽器中彈出報錯視窗,出現報錯時,Silverlight應用將停止運行。onSilverlightError函數作為預設的外部錯誤資訊處理函數,在Silverlight中是這樣調用的: <param name="onError" value="onSilverlightError" />  而通過Javascript中的  throw new Error(errMsg);  傳遞錯誤資訊到瀏覽器。 當然,開發人員可以根據需求自訂錯誤處理函數,或者修改錯誤處理函數。例如,修改errMessage為errorLocation.InnerHTML,錯誤資訊顯示也會不同。 4. <Object>標籤是調用Silverlight的主要標籤,該標籤中包含了很多屬性標籤,其中關鍵的屬性是"type",該標籤標識了該Silverlight應用所需要的Silverlight版本。在本應用中,我建立的是Silverlight 3應用,type="application/x-silverlight-2", 該標籤和Silverlight 2 的type標籤內容相同,我查看了Silverlight 3 Release Note,微軟解釋是為了Silverlight2應用升級到Silverlight3相容性,使用相同標籤內容。我看了一下Silverlight beta 2版本的標籤,是type="application/x-silverlight-2-b2"。  1 <object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%">
 2           <param name="source" value="ClientBin/Html_SilverlightChina.xap"/>
 3           <param name="onError" value="onSilverlightError" />
 4           <param name="background" value="white" />
 5           <param name="minRuntimeVersion" value="3.0.40818.0" />
 6           <param name="autoUpgrade" value="true" />
 7           <a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=3.0.40818.0" style="text-decoration:none">
 8                <img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style:none"/>
 9           </a>
10         </object>  在<Object>標籤中,<param>標籤定義了Silverlight執行個體參數。其中"source"是比較重要的一個參數,標識XAP檔案的位置。有些時候,根據項目需要,也可以將source留為空白,然後在XAML中使用Javascript代碼進行調用。對於Object的參數,我想使用一個表格來列述: 

 

參數 介紹
AutoUpgrade  允許開發人員控制終端使用者的Silverlight是否自動升級。預設為True
Background 設定應用背景顏色,預設為Null
enableFramerateCounter 是否在瀏覽器狀態列顯示當前畫面播放速率,預設為false
enableHtmlAccess 是否允許訪問瀏覽器DOM,預設為false,如果為true,將允許Silverlight訪問網頁
iniParams 初始化參數資訊,通過該參數可以從網頁傳遞參數到Silverlight應用中,在Silverlight中可以通過後台代碼擷取參數(非常有用)
minRuntimeVersion 運行該應用的最低Silverlight版本,在本例中,最低版本是Silverlight 3.0.40818.0
maxFramerate  設定最大的畫面播放速率,預設是每秒60幀
onLoad 可以在本事件中調用自訂Javascript函數
splashScreenSource 設定一個xaml檔案作為下載XAP的時候,動畫開始頁面
Source XAP路徑

 

  5. 在<Object>中有一段代碼是這樣的: <a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=3.0.40818.0" style="text-decoration:none">
               <img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style:none"/>
          </a>  這段代碼是檢測如果瀏覽器沒有安裝Silverlight用戶端外掛程式,會顯示"Get Silverlight"的圖片,提示使用者下載並安裝Silverlight用戶端。有的國內的開發人員認為每次到微軟網站下載速度太慢,可以修改"http://go.microsoft.com/fwlink/?LinkID=149156&v=3.0.40818.0"連結,讓使用者到本地或者指定串連下載Silverlight用戶端外掛程式。  我們來完成上面的項目,添加簡單代碼,完成示範, 我上傳了一個XAP檔案到cnblogs,下面我使用以下代碼調用即可。 代碼 1 <object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%">
 2           <param name="source" value="/jv9/Html_SilverlightChina.xap"/>
 3           <param name="onError" value="onSilverlightError" />
 4           <param name="background" value="white" />
 5           <param name="minRuntimeVersion" value="3.0.40818.0" />
 6           <param name="autoUpgrade" value="true" />
 7           <a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=3.0.40818.0" style="text-decoration:none">
 8                <img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style:none"/>
 9           </a>
10         </object>  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.