Evil 域

当Evil遇上先知

导航

Silverlight 学习笔记——应用程序模型

Posted on 2010-01-08 22:01  Saar  阅读(1203)  评论(0编辑  收藏  举报
Silverlight提供了一个比较清晰的应用程序模型。如果要实现一个简单的Silverlight应用,我们可以将这个模型抛之脑后。但是,如果想实现一些特殊的功能,例如:传递自定义参数、使用自定义的载入动画,就需要对Silverlight的应用程序模型有所了解。了解Silverlight的应用程序模型,我们才能了解Silverlight应用程序的始末。
  • Silverlight生命周期

  一个Silverlight应用程序,从开始请求到完全载入,一般经过6个步骤:

    • 用户请求HTML页面,HTML页面中含有Silverlight的入口;
    • 浏览器下载Silverlight插件和对应的XAP文件;
    • Silverlight插件开始工作,读取XAP文件里的AppManifast.xml文件来载入需要用到的Assemblies;
    • Silverlight插件创建一个App类的实例;
    • Application的默认构造函数引发Startup事件;
    • Silverlight应用程序处理Startup事件。

  直到Silverlight应用遇到异常或者用户关闭页面,Silverlight应用程序结束。

 

  • 应用程序入口

  由Silverlight的生命周期看到,Silverlight插件下载好了以后创建的第一个类就应该是App类,因此,App类的默认构造函数,也就成为了Silverlight的应用程序入口了。让我们打开app.xaml.cs来瞥一眼App类的默认构造函数:

 public partial class App : Application
{
public App()
{
this.Startup += this.Application_Startup;
this.Exit += this.Application_Exit;
this.UnhandledException += this.Application_UnhandledException;

InitializeComponent();
}
}

  因此,App什么都没有做,只是傻傻的添加了三个事件的处理程序,分别对应了Silverlight的启动、退出和出现异常。而真正意义上的代码,发生在应用程序的Startup事件中:

 private void Application_Startup(object sender, StartupEventArgs e)
{
this.RootVisual = new MainPage();
}

  这里虽然只有一行代码,但是,它调用了我们的主窗口的构造函数,并且,将生成的对象赋给了App对象的RootVisual属性。这样,Silverlight应用程序就被引入到用户的代码片段中。

 

  • 应用程序事件

  在应用程序载入的过程中,我们看到App类的构造函数中注册了3个应用程序事件的处理程序。App类中有很大篇幅是用来处理这三个重要的应用程序事件的。

    • Startup

      Silverlight应用程序启动时触发,前节中已经涉及到了其处理程序,它用于生成启动页面类的实例并且赋值给Application.Current.RootVisual。另外,我们可以通过e来获取初始化的参数。方法如下:

  首先,把需要的参数添加到HTML中:

 <object data="data:application/x-silverlight-2," type="application/x-silverlight-2"
width="100%" height="100%">
<param name="source" value="ClientBin/ApplicationModel.xap" />
<param name="onError" value="onSilverlightError" />
<param name="background" value="white" />
<param name="minRuntimeVersion" value="3.0.40818.0" />
<param name="autoUpgrade" value="true" />
<param name="initParams" value="initWords1=Hello,initWords2=World" />
<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=161376" alt="Get Microsoft Silverlight"
style="border-style: none" />
</a>
</object>

  这个参数必须以“initParams”命名,参数的键与值之间以等号分隔;多个参数之间用逗号分隔。然后,我们就可以通过e.InitParams对初始化参数进行访问了,例如:

 private void Application_Startup(object sender, StartupEventArgs e)
{
this.RootVisual = new MainPage();
if (e.InitParams.ContainsKey("initWords2"))
{
TextBlock tb = new TextBlock();
tb.Text = e.InitParams["initWords2"].ToString();
((MainPage)this.RootVisual).LayoutRoot.Children.Add(tb);
}
}

  我们取出了参数列表中的第二个参数,把它赋给了一个TextBlock并且添加到了MainPage的Grid中去。

 

    • Exit

      Exit事件在Silverlight退出时,会被触发。这一事件可以给程序一个最后的机会来保存用户相关的数据,例如记录当前用户的偏好等。

    • UnhandledException

      顾名思义,UnhandledException会发生在有未处理的异常被系统捕获到的时候触发。Silverlight为我们生成了一些默认的处理程序:

 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 it 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); });
}
}

      e.Handled来表明这个异常是不是已经被处理了。当Debug时,e.Handled会被设置为true,并且通过一个匿名方法异步的调用ReportErrorToDOM方法。而这个方法,则把异常信息以显示出来:

 private void ReportErrorToDOM(ApplicationUnhandledExceptionEventArgs e)
{
try
{
string errorMsg = e.ExceptionObject.Message + e.ExceptionObject.StackTrace;
errorMsg = errorMsg.Replace('"', '\'').Replace("\r\n", @"\n");

System.Windows.Browser.HtmlPage.Window.Eval("throw new Error(\"Unhandled Error in Silverlight 2 Application " + errorMsg + "\");");
}
catch (Exception)
{
}
}

 

  • 自定义载入动画

  在浏览器下载XAP文件时,如果下载时间超过500毫秒,就会加载一个载入动画。Silverlight提供了一个默认的装载动画,但是,大家都用默认动画,会让人觉得千篇一律。Silverlight在应用程序模型中提供了对自定义载入动画的支持。

  要完成一个自定义动画,需要三件东西:XAML、javascript和Silverlight参数。

  我们需要通过XAML来构造载入动画的界面。由于载入动画发生在XAP文件被下载之前,因此,它必须独立于XAP文件;载入时,系统还没有能够创建SLR运行的环境,因此,我们无法使用C#或者VB.NET等托管代码。这也是为什么我们需要javascript。javascript提供对当前下载进度的追踪,并且更新界面。Sliverlight参数则指定自定义动画所在的XAML文件和所使用的javascript函数。

  让我们来小实战一把。首先,准备一个载入界面的XAML。我们可以通过添加Silverlight模板里的“Silverlight JScript Page”来添加一个XAML页面。这个XAML很简单,就提供了一个简单的进度条:

<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<StackPanel VerticalAlignment="Center">
<Grid>
<Rectangle x:Name="progressBarBackground" Fill="White" Stroke="Black" StrokeThickness="1" Height="30" Width="200"></Rectangle>
<Rectangle x:Name="progressBar" Fill="Green" Height="28" Width="0"></Rectangle>
</Grid>
<TextBlock x:Name="progressText" HorizontalAlignment="Center" Text="0% downloaded ..."></TextBlock>
</StackPanel>
</Grid>

  然后,我们在包含有Silverlight的HTML页面中,添加Javascript来处理下载进度:

 <script type="text/javascript">
function onSourceDownloadProgressChanged(sender, eventArgs) {
sender.findName("progressText").Text = Math.round((eventArgs.progress * 100)) + "% downloaded ...";
sender.findName("progressBar").Width = eventArgs.progress * sender.findName("progressBarBackground").Width;
}
</script>

  这里,我们通过findName来获得XAML中创建的控件,然后,通过eventArgs.progress来计划出下载量,并且对progressBar和progressText进行调整。

  最后,我们通过参数,把自定义载入动画的XAML和javascript代码添加到Silverlight对象中:

 <div id="silverlightControlHost">
<object data="data:application/x-silverlight-2," type="application/x-silverlight-2"
width="100%" height="100%">
<param name="source" value="ClientBin/CustomSplashDemo.xap" />
<param name="onError" value="onSilverlightError" />
<param name="background" value="white" />
<param name="minRuntimeVersion" value="3.0.40818.0" />
<param name="autoUpgrade" value="true" />

<param name="splashscreensource" value="NewSplash.xaml" />
<param name="onsourcedownloadprogresschanged" value="onSourceDownloadProgressChanged" />

<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>
</object>
<iframe id="_sl_historyFrame" style="visibility: hidden; height: 0px; width: 0px;
border: 0px"></iframe>
</div>

  由于我们的Silverlight太小,看不到载入动画。这里有一个技巧,我们可以把startup事件处理程序中的this.RootVisual = new MainPage();注释掉,这样,Silverlight在载入完成后,就不会用MainPage的对象覆盖住自定义的载入动画,我们就可以看到载入动画的效果了。

  由于VS2010 Beta2存在BUG,添加了XAML以后,WebSite无法编译,最后,只能用VS2008完成这个例子。


 

  • 写在最后

  Silverlight从插件被下载到正常运行,一共6个步骤。这六步中,会触发Startup事件;到遇到用户未处理异常时,会触发UnhandledException事件;而Silverlight退出时,会触发Exit事件。

  Silverlight为自定义载入动画提供了良好的支持。只是由于其载入阶段较早,无法使用托管代码。幸运的是,对应的javascript还算比较直接、明了。