by Cameron Albert
11. March 2008 23:01
If you are using the <asp:Silverlight/> control to host your Silverlight 2.0 apps from an ASPX page and want to be able to provide a custom splash screen as outlined in Pete Brown's post Xap! App! Pow! Packaging and Application Startup in Silverlight 2 Beta 1 - Part 2 I created the following override that allows you to set the SplashScreenSource and OnSourceDownloadProgressChanged values from within the control itself.
Here is the source for the control:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Lionsguard.Web.UI.SilverlightControls
{
[ToolboxData(@"<{0}:Silverlight runat=""server""></{0}:Silverlight>")]
public class Silverlight : System.Web.UI.SilverlightControls.Silverlight
{
protected override IDictionary<string, string> GetSilverlightParameters()
{
IDictionary<string, string> dictionary = base.GetSilverlightParameters();
if (!String.IsNullOrEmpty(this.SplashScreenSource))
{
dictionary.Add("SplashScreenSource", this.SplashScreenSource);
}
if (!String.IsNullOrEmpty(this.OnSourceDownloadProgressChanged))
{
dictionary.Add("OnSourceDownloadProgressChanged", this.OnSourceDownloadProgressChanged);
}
return dictionary;
}
[Category("Behavior"), Browsable(true), DefaultValue(""), Description("")]
public virtual string SplashScreenSource
{
get
{
return (((string)this.ViewState["SplashScreenSource"]) ?? string.Empty);
}
set
{
this.ViewState["SplashScreenSource"] = value;
}
}
[Category("Behavior"), Browsable(true), DefaultValue(""), Description("")]
public virtual string OnSourceDownloadProgressChanged
{
get
{
return (((string)this.ViewState["OnSourceDownloadProgressChanged"]) ?? string.Empty);
}
set
{
this.ViewState["OnSourceDownloadProgressChanged"] = value;
}
}
}
}
And here is the way that I am using it in my code:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Perenthia.Web._Default" %>
<%@ Register Assembly="Lionsguard.Web.Silverlight" Namespace="Lionsguard.Web.UI.SilverlightControls" TagPrefix="lg" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Perenthia</title>
<script language="javascript" type="text/javascript" src="/Common/Scripts/Silverlight.js"></script>
<script language="javascript" type="text/javascript" src="/Common/Scripts/Splash.js"></script>
</head>
<body>
<form id="frmMain" runat="server">
<asp:ScriptManager ID="scriptManager" runat="server"></asp:ScriptManager>
<div>
<lg:Silverlight ID="silverlight1" runat="server" Source="~/ClientBin/Perenthia.xap"
Version="2.0" Width="800" Height="500" SplashScreenSource="~/Common/Xaml/Splash.xaml"
OnSourceDownloadProgressChanged="onSourceDownloadProgressChanged" />
</div>
</form>
</body>
</html>
The Splash.js file contains the following JavaScript:
function onSourceDownloadProgressChanged(sender, eventArgs)
{
sender.findName("uxStatus").Text = "Loading: " + Math.round((eventArgs.progress * 1000)) / 10 + "%";
sender.findName("uxProgressBar").ScaleY = eventArgs.progress * 356;
}
Both the Splash.xaml and associated JS are from the Silverlight Beta 1 Quick Start for Displaying a Splash Screen While Loading a Silverlight-Based Application. I will eventually replace the splash with my own Xaml but for now this one works OK for testing. :)
**EDIT: The XAP file and the XAML file used to render the splash screen must reside in the same directory, be that the ClientBin or the root.
** BETA 2 EDIT: Microsoft has added the SplashScreenSource property and OnPluginSourceDownloadProgressChanged event to their Silverlight control. Kind of makes this one obsolete. I will post more about it soon.