Debugging Silverlight 1.1
I was thinking to share my experience debugging Silverlight with Visual Studio 2008. Setting up en environment for Silverlight is easy, just select new project and Silverlight. You'll see the drop down box on the right hand side, select the correct one to see the templates.
You can always compile the XAML file and move it into a Web Form or for debugging I would recommend to use an HTML page:
<html xmlns="http://www.w3.org/1999/xhtml">
<!-- saved from url=(0014)about:internet -->
<head>
<title>Map Viewer</title>
<script type="text/javascript" src="Silverlight.js"></script>
<script type="text/javascript" src="Default.html.js"></script>
<style type="text/css">
#Text1
{
width: 465px;
}
</style>
</head>
<!-- Give the keyboard focus to the Silverlight control by default -->
<body onload="pageLoaded();" style="margin:0px;">
<div id="SilverlightControlHost" >
<script type="text/javascript">
createSilverlight();
</script>
</div>
</body>
</html>
Its that simple, the template will create one for you, to call the createSilverlight() javascript method required to run.
In Silverlight 1.1 you can use C# to write the code for the control instead of using javascript like in 1.0. Make sure to remember you are writing only client side code, even if you are using code behind does not mean the server side code is available for you. The class I found to communicate with the server is the Downloader class:
using System;
using System.Security;
namespace System.Windows
{
public sealed class Downloader : DependencyObject
{
public static readonly DependencyProperty DownloadProgressProperty;
public static readonly DependencyProperty ResponseTextProperty;
public static readonly DependencyProperty StatusProperty;
public static readonly DependencyProperty StatusTextProperty;
public static readonly DependencyProperty UriProperty;
public Downloader();
public double DownloadProgress { get; set; }
public string ResponseText { get; set; }
public int Status { get; set; }
public string StatusText { get; set; }
public Uri Uri { get; set; }
public event EventHandler Completed;
public event ErrorEventHandler DownloadFailed;
public event EventHandler DownloadProgressChanged;
[SecuritySafeCritical]
public void Abort();
[SecuritySafeCritical]
public string GetResponseText(string PartName);
[SecuritySafeCritical]
public void Open(string verb, Uri URI);
[SecuritySafeCritical]
public void Send();
}
}
If you have been using AJAX, this will be natural to you. This is how to implemented:
var downloader = new Downloader();
downloader.Completed += new EventHandler(downloader_Completed);
downloader.DownloadFailed += new ErrorEventHandler(downloader_DownloadFailed);
downloader.Open("GET", new Uri(HtmlPage.DocumentUri, sUrlRequest));
downloader.Send();
You can substitute var for Downloader. The sUrlRequest is a URL to download or a web service. I find easy to download if I use a web page that a web service. Then again they both work. Downloader cannot be cross domain, so make sure the website/web service is in the same computer.
The callback function when the downloader is complete looks like this, if fails, downloader_DownloadFailed will be called with the same signature:
private void downloader_Completed(object sender, EventArgs e)
{
Downloaderdownloader = sender as Downloader;
TextBlock text1 = _helpCanvas.FindName("Textbox1") as TextBlock;
TextBlock text2 = _helpCanvas.FindName("Textbox2") as TextBlock;
text1.Text = "Finished";
text2.Text = downloader.ResponseText;
}
You can interact with client side code without postbacks, as if you remember, Downloader uses Ajax to make the calls.
Debugging Client Side
First thing that jumps to you while you debugging Silverlight using VS2008 is when running the debugger you'll find this error:
Silverlight won't run as a document, as needs to run under IIS, just click continue twice and change the browser url to point to a virtual directory where your solution is kept.
The most annoying problem I found is debugging and trying to step into a method, if there is no breakpoint inside that method, the debugger will just step over.
Hope this helps people debugging silverlight.
Cheers
Al



Comments
DotNetKicks.com said on 10.26.2007 at 2:27 PM
You've been kicked (a good thing) - Trackback from DotNetKicks.com
Ghillie Suits » Debugging Silverlight 1.1 - Al Pascual said on 10.26.2007 at 4:54 PM
Pingback from Ghillie Suits » Debugging Silverlight 1.1 - Al Pascual