本文主要包含:
1.silverlight应用程序的建立及第一个hello world程序。
2.hello world的简单介绍。
1.silverlight应用程序的建立及第一个hello world程序
2.hello world的简单介绍
第一个hello world程序虽然简单,但是却包含了silverlight application中的所有的元素。
2.1,asp.net网站
asp.net网站用来承载silverlight程序,在工程中生成两中页面,aspx和html,分别来说明silverlight是如何在上述两种页面中加载的。查看代码可知,silverlight程序最终是通过object形式来加入页面中的。代码如下,其中onSilverlightError是在文件上面定义的js函数:
<object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%"> <param name="source" value="ClientBin/HelloWorldBlend.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" mce_href="http://go.microsoft.com/fwlink/?LinkID=149156&v=4.0.50826.0" style="text-decoration: none;" mce_style="text-decoration: none;"> <img src="http://go.microsoft.com/fwlink/?LinkID=161376" mce_src="http://go.microsoft.com/fwlink/?LinkID=161376" alt="Get Microsoft Silverlight" style="border-style: none" mce_style="border-style: none"/> </a> </object>
2.2,silverlight application
App.xaml:
public App() { this.Startup += this.Application_Startup; this.Exit += this.Application_Exit; this.UnhandledException += this.Application_UnhandledException; InitializeComponent(); } private void Application_Startup(object sender, StartupEventArgs e) { // 这里添加出示启动的窗体 this.RootVisual = new Anmi(); } private void Application_Exit(object sender, EventArgs e) { } private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e) { // If the app is running outside of the debugger then report the exception using // the browser's exception mechanism. On IE this will display as a yellow alert // icon in the status bar and Firefox will display a script error. if (!System.Diagnostics.Debugger.IsAttached) { // NOTE: This will allow the application to continue running after an exception has been thrown // but not handled. // For production applications this error handling should be replaced with something that will // report the error to the website and stop the application. e.Handled = true; Deployment.Current.Dispatcher.BeginInvoke(delegate { ReportErrorToDOM(e); }); } }
MainPage.xaml是silverlight应用程序默认的用户控件,编译运行时会自动托管到asp.net的网站中。