Silverlight3學習筆記(2):Silverlight初接觸
阿新 • • 發佈:2020-10-13
在上一篇中講述瞭如何安裝適合自己需要的VS2010以及相關幫助文件,由於在VS2010中幫助文件的查詢方式不同於以往的VS,在上篇中還介紹了H3Viewer這個軟體,它是一個免費的軟體,安裝它之後可以在安裝了VS2010幫助文件之後以類似於以前檢視MSDN的方式檢視幫助文件。除此之外,在上一篇還比較了幾種常見的RIA技術,比如Java Applet、Flash及Silverlight,以及它們與普通Windows應用程式在執行機制上的區別和限制。在這一篇開始涉及到Silverlight了,這一篇主要是講Silverlight的開發工具VS2010以及XAML語言等知識。
建立Silverlight專案
當我們建立一個 Silverlight專案時會提示是否建立一個承載專案,如下圖所示:
由於在VS2010中已經直接支援建立ASP.NET MVC2的Web專案,所以承載Silverlight的Web專案型別有三種:ASP.NET Web應用程式專案、ASP.NET 網站及ASP.NET MVC Web應用程式專案。ASP.NET Web應用程式專案可以提供與Visual Studio .NET 2003 Web 專案相同的 Web 專案語義,它的編譯模型與 Visual Studio .NET 2003 編譯模型類似。專案中的所有程式碼檔案(獨立檔案、程式碼隱藏檔案以及類檔案)將被編譯成單個程式集並存儲在 Bin 目錄中。由於編譯會建立單個程式集,因此可以指定程式集名稱和版本等屬性。如果我們僅僅是開發 Silverlight的話,這個可以隨便選擇一種型別,以便能在網頁中檢視Silverlight的執行效果。
VS2010介面
建立Silverlight專案成功之後可以看到如下的介面:
在VS2010中可以直接從工具箱中向Silverlight介面中拖控制元件。在開發ASP.NET時一個頁面會分為兩部分.aspx.cs和.cs,前者包含了設計程式碼,後者則是包含業務邏輯程式碼,和ASP.NET開發一樣,Silverlight的專案中每個控制元件或者頁面也是分為.xaml和.xaml.cs,.xaml程式碼也是包含設計程式碼,.xaml.cs則是包含業務邏輯程式碼。在每個Silverlight專案中預設會有一個 app.xaml檔案,這個檔案有點類似於WinForm專案中的program.cs類,包含了專案啟動時的設定,在建立一個Silverlight專案成功後app.xam.cs的程式碼如下:
- usingSystem;
- usingSystem.Collections.Generic;
- usingSystem.Linq;
- usingSystem.Net;
- usingSystem.Windows;
- usingSystem.Windows.Controls;
- usingSystem.Windows.Documents;
- usingSystem.Windows.Input;
- usingSystem.Windows.Media;
- usingSystem.Windows.Media.Animation;
- usingSystem.Windows.Shapes;
- namespaceSilverlightDemo1
- {
- publicpartialclassApp:Application
- {
- publicApp()
- {
- this.Startup+=this.Application_Startup;
- this.Exit+=this.Application_Exit;
- this.UnhandledException+=this.Application_UnhandledException;
- InitializeComponent();
- }
- privatevoidApplication_Startup(objectsender,StartupEventArgse)
- {
- this.RootVisual=newMainPage();
- }
- privatevoidApplication_Exit(objectsender,EventArgse)
- {
- }
- privatevoidApplication_UnhandledException(objectsender,ApplicationUnhandledExceptionEventArgse)
- {
- //如果應用程式是在偵錯程式外執行的,則使用瀏覽器的
- //異常機制報告該異常。在IE上,將在狀態列中用一個
- //×××警報圖示來顯示該異常,而Firefox則會顯示一個指令碼錯誤。
- if(!System.Diagnostics.Debugger.IsAttached)
- {
- //注意:這使應用程式可以在已引發異常但尚未處理該異常的情況下
- //繼續執行。
- //對於生產應用程式,此錯誤處理應替換為向網站報告錯誤
- //並停止應用程式。
- e.Handled=true;
- Deployment.Current.Dispatcher.BeginInvoke(delegate{ReportErrorToDOM(e);});
- }
- }
- privatevoidReportErrorToDOM(ApplicationUnhandledExceptionEventArgse)
- {
- try
- {
- stringerrorMsg=e.ExceptionObject.Message+e.ExceptionObject.StackTrace;
- errorMsg=errorMsg.Replace('"','\'').Replace("\r\n",@"\n");
- System.Windows.Browser.HtmlPage.Window.Eval("thrownewError(\"UnhandledErrorinSilverlightApplication"+errorMsg+"\");");
- }
- catch(Exception)
- {
- }
- }
- }
- }
- <!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <htmlxmlns="http://www.w3.org/1999/xhtml">
- <!--savedfromurl=(0014)about:internet-->
- <head>
- <title>SilverlightDemo1</title>
- <styletype="text/css">
- html,body{
- height:100%;
- overflow:auto;
- }
- body{
- padding:0;
- margin:0;
- }
- #silverlightControlHost{
- height:100%;
- text-align:center;
- }
- </style>
- <scripttype="text/javascript">
- functiononSilverlightError(sender,args){
- varappSource="";
- if(sender!=null&&sender!=0){
- appSource=sender.getHost().Source;
- }
- varerrorType=args.ErrorType;
- variErrorCode=args.ErrorCode;
- if(errorType=="ImageError"||errorType=="MediaError"){
- return;
- }
- varerrMsg="應用程式中未處理的錯誤"+appSource+"\n";
- errMsg+="程式碼:"+iErrorCode+"\n";
- errMsg+="類別:"+errorType+"\n";
- errMsg+="訊息:"+args.ErrorMessage+"\n";
- if(errorType=="ParserError"){
- errMsg+="檔案:"+args.xamlFile+"\n";
- errMsg+="行:"+args.lineNumber+"\n";
- errMsg+="位置:"+args.charPosition+"\n";
- }
- elseif(errorType=="RuntimeError"){
- if(args.lineNumber!=0){
- errMsg+="行:"+args.lineNumber+"\n";
- errMsg+="位置:"+args.charPosition+"\n";
- }
- errMsg+="方法名稱:"+args.methodName+"\n";
- }
- thrownewError(errMsg);
- }
- </script>
- </head>
- <body>
- <formid="form1"runat="server"style="height:100%">
- <divid="silverlightControlHost">
- <objectdata="data:application/x-silverlight-2,"type="application/x-silverlight-2"width="100%"height="100%">
- <paramname="source"value="SilverlightDemo1.xap"/>
- <paramname="onError"value="onSilverlightError"/>
- <paramname="background"value="white"/>
- <paramname="minRuntimeVersion"value="3.0.40818.0"/>
- <paramname="autoUpgrade"value="true"/>
- <ahref="http://go.microsoft.com/fwlink/?LinkID=149156&v=3.0.40818.0"style="text-decoration:none">
- <imgsrc="http://go.microsoft.com/fwlink/?LinkId=108181"alt="獲取MicrosoftSilverlight"style="border-style:none"/>
- </a>
- </object><iframeid="_sl_historyFrame"style="visibility:hidden;height:0px;width:0px;border:0px"></iframe></div>
- </form>
- </body>
- </html>
轉載於:https://blog.51cto.com/zhoufoxcn/391313