MSDN Magazine - January 2009 - (Page 36) Figure 5 Download Complete void OnCompleted(object sender, OpenReadCompletedEventArgs e) { if (PackageDownloaded == null) return; if (e.Error != null) return; // Load a particular assembly from the XAP package Assembly a = GetAssemblyFromPackage(assemblyName, e.Result); // Get an instance of the XAML object object page = a.CreateInstance(className); // Fire the event XapEventArgs args = new XapEventArgs(); args.DownloadedContent = page as UserControl; XapDownloaded(this, args); Figure 6 Extracting an Assembly from a XAP Package Assembly GetAssemblyFromPackage(string assemblyName, Stream xap) { // Local variables Uri assemblyUri = null; StreamResourceInfo resPackage = null; StreamResourceInfo resAssembly = null; AssemblyPart part = null; // Initialize assemblyUri = new Uri(assemblyName, UriKind.Relative); resPackage = new StreamResourceInfo(xap, null); resAssembly = Application.GetResourceStream( resPackage, assemblyUri); // Extract an assembly part = new AssemblyPart(); Assembly a = part.Load(assemblySri.Stream); return a; } } JavaScript downloader object is essentially a script-callable wrapper for WebClient. Processing XAP Content The Downloader class uses WebClient to download a XAP package and then extracts the assembly that contains the application from the zipped stream and instantiates the specified root class. All this logic is hidden behind a relatively simple programming facade made of a LoadPackage method and a XapDownloaded event. The method has the following signature: public void LoadPackage( string xapURL, string assemblyName, string className); sembly from the package and gets an instance of the user control class in the codebehind of the XAML block to add. Next, a custom event is fired to the caller so that the XAML subtree can be processed. At this point, one thing remains to expand on: the details of how an assembly is extracted from the zipped stream downloaded from the URL. This code is shown in Figure 6. This is boilerplate code you use to extract an assembly from a XAP package. StreamResourceInfo and Application.GetResourceStream are also common tools for extracting resource packages such as images or media content from the current XAP. It receives the URL to the package, the name of the assembly in the package to extract, and the name of the class to instantiate. As mentioned, this class is the codebehind of the XAML interface file. The XapDownloaded event is fired when the downloader class has finished with the XAP processing and has the user control ready for the client application. Here’s the definition of the event: public event EventHandler XapDownloaded; Summing It Up The event argument class returns the following information: public class XapEventArgs : EventArgs { public UserControl DownloadedContent; } Let’s dig out the logic of the LoadPackage method. The method employs the binary interface of WebClient to point to the XAP package: void LoadPackage(string package, string asm, string cls) { assemblyName = asm; className = cls; Uri address = new Uri(package, UriKind.RelativeOrAbsolute); WebClient client = new WebClient(); client.OpenReadCompleted += new OpenReadCompletedEventHandler(OnCompleted); client.OpenReadAsync(address); Silverlight applications are expected to download and install quickly and run fast on the user’s machine. Managed code outperforms interpreted JavaScript, so performance will indeed be snappy. But because downloading large Silverlight applications, or applications that require large external graphic and media content, can create undesired latency, breaking the download into steps helps. It is even better if you program these steps to occur on demand. The WebClient class offers an effective and asynchronous programming API to download any sort of resources you can access over the Internet. The extensible model for the Silverlight object model allows you to incorporate any changes in the existing structure quickly. Finally, the stream-based API centered around the StreamResourceInfo class makes it easy to extract content from the resources of assemblies and XAP packages. Any downloaded resource is cached by the browser. This sort of caching is not permanent, though. In Part 2 of this column, I’ll use isolated storage to make any downloaded content persist longer on the user’s machine, thus saving many potential round-trips. For more Silverlight goodies, see Jeff Prosise’s Wicked Code column “Silverlight Tips, Tricks, and Best Practices” at msdn.microsoft.com/ magazine/cc164249. } The download proceeds asynchronously and fires the OpenReadCompleted event when it is finished. Figure 5 shows the implementation for the handler of the event. If no error occurred, a helper function extracts the specified as36 msdn magazine Dino Esposito is an architect at IDesign and the coauthor of Microsoft .NET: Architecting Applications for the Enterprise (Microsoft Press, 2008). Based in Italy, Dino is a frequent speaker at industry events worldwide. You can join his blog at weblogs.asp.net/despos. Cutting Edge http://msdn.microsoft.com/magazine/cc164249 http://msdn.microsoft.com/magazine/cc164249 http://weblogs.asp.net/despos
Table of Contents Feed for the Digital Edition of MSDN Magazine - January 2009 Toolbox CLR Inside Out Basic Instincts Cutting Edge Test Run First Look Geneva Framework Silverlight Windows Mobile Service Station Security Briefs Extreme ASP.NET Foundations .NET Matters { End Bracket } MSDN Magazine - January 2009 MSDN Magazine - January 2009 - (Page Intro) MSDN Magazine - January 2009 - (Page Cover1) MSDN Magazine - January 2009 - (Page Cover2) MSDN Magazine - January 2009 - (Page 1) MSDN Magazine - January 2009 - (Page 2) MSDN Magazine - January 2009 - (Page 3) MSDN Magazine - January 2009 - (Page 4) MSDN Magazine - January 2009 - (Page 5) MSDN Magazine - January 2009 - (Page 6) MSDN Magazine - January 2009 - (Page 7) MSDN Magazine - January 2009 - (Page 8) MSDN Magazine - January 2009 - Toolbox (Page 9) MSDN Magazine - January 2009 - Toolbox (Page 10) MSDN Magazine - January 2009 - Toolbox (Page 11) MSDN Magazine - January 2009 - Toolbox (Page 12) MSDN Magazine - January 2009 - Toolbox (Page 13) MSDN Magazine - January 2009 - Toolbox (Page 14) MSDN Magazine - January 2009 - CLR Inside Out (Page 15) MSDN Magazine - January 2009 - CLR Inside Out (Page 16) MSDN Magazine - January 2009 - CLR Inside Out (Page 17) MSDN Magazine - January 2009 - CLR Inside Out (Page 18) MSDN Magazine - January 2009 - CLR Inside Out (Page 19) MSDN Magazine - January 2009 - CLR Inside Out (Page 20) MSDN Magazine - January 2009 - Basic Instincts (Page 21) MSDN Magazine - January 2009 - Basic Instincts (Page 22) MSDN Magazine - January 2009 - Basic Instincts (Page 23) MSDN Magazine - January 2009 - Basic Instincts (Page 24) MSDN Magazine - January 2009 - Basic Instincts (Page 25) MSDN Magazine - January 2009 - Basic Instincts (Page 26) MSDN Magazine - January 2009 - Basic Instincts (Page 27) MSDN Magazine - January 2009 - Basic Instincts (Page 28) MSDN Magazine - January 2009 - Basic Instincts (Page 29) MSDN Magazine - January 2009 - Basic Instincts (Page 30) MSDN Magazine - January 2009 - Cutting Edge (Page 31) MSDN Magazine - January 2009 - Cutting Edge (Page 32) MSDN Magazine - January 2009 - Cutting Edge (Page 33) MSDN Magazine - January 2009 - Cutting Edge (Page 34) MSDN Magazine - January 2009 - Cutting Edge (Page 35) MSDN Magazine - January 2009 - Cutting Edge (Page 36) MSDN Magazine - January 2009 - Cutting Edge (Page 37) MSDN Magazine - January 2009 - Cutting Edge (Page 38) MSDN Magazine - January 2009 - Test Run (Page 39) MSDN Magazine - January 2009 - Test Run (Page 40) MSDN Magazine - January 2009 - Test Run (Page 41) MSDN Magazine - January 2009 - Test Run (Page 42) MSDN Magazine - January 2009 - Test Run (Page 43) MSDN Magazine - January 2009 - Test Run (Page 44) MSDN Magazine - January 2009 - Test Run (Page 45) MSDN Magazine - January 2009 - Test Run (Page 46) MSDN Magazine - January 2009 - Test Run (Page 47) MSDN Magazine - January 2009 - Test Run (Page 48) MSDN Magazine - January 2009 - Test Run (Page 49) MSDN Magazine - January 2009 - First Look (Page 50) MSDN Magazine - January 2009 - First Look (Page 51) MSDN Magazine - January 2009 - First Look (Page 52) MSDN Magazine - January 2009 - First Look (Page 53) MSDN Magazine - January 2009 - First Look (Page 54) MSDN Magazine - January 2009 - First Look (Page 55) MSDN Magazine - January 2009 - First Look (Page 56) MSDN Magazine - January 2009 - First Look (Page 57) MSDN Magazine - January 2009 - First Look (Page 58) MSDN Magazine - January 2009 - First Look (Page 59) MSDN Magazine - January 2009 - First Look (Page 60) MSDN Magazine - January 2009 - First Look (Page 61) MSDN Magazine - January 2009 - First Look (Page 62) MSDN Magazine - January 2009 - First Look (Page 63) MSDN Magazine - January 2009 - Geneva Framework (Page 64) MSDN Magazine - January 2009 - Geneva Framework (Page 65) MSDN Magazine - January 2009 - Geneva Framework (Page 66) MSDN Magazine - January 2009 - Geneva Framework (Page 67) MSDN Magazine - January 2009 - Geneva Framework (Page 68) MSDN Magazine - January 2009 - Geneva Framework (Page 69) MSDN Magazine - January 2009 - Geneva Framework (Page 70) MSDN Magazine - January 2009 - Geneva Framework (Page 71) MSDN Magazine - January 2009 - Geneva Framework (Page 72) MSDN Magazine - January 2009 - Geneva Framework (Page 73) MSDN Magazine - January 2009 - Geneva Framework (Page 74) MSDN Magazine - January 2009 - Silverlight (Page 75) MSDN Magazine - January 2009 - Silverlight (Page 76) MSDN Magazine - January 2009 - Silverlight (Page 77) MSDN Magazine - January 2009 - Silverlight (Page 78) MSDN Magazine - January 2009 - Silverlight (Page 79) MSDN Magazine - January 2009 - Silverlight (Page 80) MSDN Magazine - January 2009 - Silverlight (Page 81) MSDN Magazine - January 2009 - Silverlight (Page 82) MSDN Magazine - January 2009 - Silverlight (Page 83) MSDN Magazine - January 2009 - Silverlight (Page 84) MSDN Magazine - January 2009 - Silverlight (Page 85) MSDN Magazine - January 2009 - Silverlight (Page 86) MSDN Magazine - January 2009 - Silverlight (Page 87) MSDN Magazine - January 2009 - Windows Mobile (Page 88) MSDN Magazine - January 2009 - Windows Mobile (Page 89) MSDN Magazine - January 2009 - Windows Mobile (Page 90) MSDN Magazine - January 2009 - Windows Mobile (Page 91) MSDN Magazine - January 2009 - Windows Mobile (Page 92) MSDN Magazine - January 2009 - Service Station (Page 93) MSDN Magazine - January 2009 - Service Station (Page 94) MSDN Magazine - January 2009 - Service Station (Page 95) MSDN Magazine - January 2009 - Service Station (Page 96) MSDN Magazine - January 2009 - Service Station (Page 97) MSDN Magazine - January 2009 - Service Station (Page 98) MSDN Magazine - January 2009 - Security Briefs (Page 99) MSDN Magazine - January 2009 - Security Briefs (Page 100) MSDN Magazine - January 2009 - Security Briefs (Page 101) MSDN Magazine - January 2009 - Security Briefs (Page 102) MSDN Magazine - January 2009 - Extreme ASP.NET (Page 103) MSDN Magazine - January 2009 - Extreme ASP.NET (Page 104) MSDN Magazine - January 2009 - Extreme ASP.NET (Page 105) MSDN Magazine - January 2009 - Extreme ASP.NET (Page 106) MSDN Magazine - January 2009 - Extreme ASP.NET (Page 107) MSDN Magazine - January 2009 - Extreme ASP.NET (Page 108) MSDN Magazine - January 2009 - Foundations (Page 109) MSDN Magazine - January 2009 - Foundations (Page 110) MSDN Magazine - January 2009 - Foundations (Page 111) MSDN Magazine - January 2009 - Foundations (Page 112) MSDN Magazine - January 2009 - Foundations (Page 113) MSDN Magazine - January 2009 - Foundations (Page 114) MSDN Magazine - January 2009 - Foundations (Page 115) MSDN Magazine - January 2009 - .NET Matters (Page 116) MSDN Magazine - January 2009 - .NET Matters (Page 117) MSDN Magazine - January 2009 - .NET Matters (Page 118) MSDN Magazine - January 2009 - .NET Matters (Page 119) MSDN Magazine - January 2009 - { End Bracket } (Page 120) MSDN Magazine - January 2009 - { End Bracket } (Page Cover3) MSDN Magazine - January 2009 - { End Bracket } (Page Cover4)
For optimal viewing of this digital publication, please enable JavaScript and then refresh the page. If you would like to try to load the digital publication without using Flash Player detection, please click here.