Click here to Skip to main content
15,895,667 members
Articles / Web Development / ASP.NET

Download "XAP" Packages on Demand in Silverlight

Rate me:
Please Sign up or sign in to vote.
4.60/5 (5 votes)
8 Oct 2010CPOL8 min read 51.1K   1.4K   26  
This article introduces a method to download and use "XAP" packages on demand in Silverlight.
using System;
using System.Net;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Reflection;
using System.Windows.Resources;

namespace XapLoader
{
    public partial class Splash : UserControl
    {
        private int downloadProgress;
        public Splash()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(Splash_Loaded);
            this.SizeChanged += new SizeChangedEventHandler((Object sender,
                    SizeChangedEventArgs e)
                => { UpdateProgressBar(); });
        }

        private void Splash_Loaded(object sender, RoutedEventArgs e)
        {
            string contentUri = Application.Current.Host.Source.AbsoluteUri.Replace(
                    "XapLoader", "XapContent");

            WebClient wc = new WebClient();
            wc.OpenReadCompleted += new OpenReadCompletedEventHandler(wc_OpenReadCompleted);
            wc.DownloadProgressChanged
                += new DownloadProgressChangedEventHandler(wc_DownloadProgressChanged);
            wc.OpenReadAsync(new Uri(contentUri));
        }

        private void wc_OpenReadCompleted(Object sender, OpenReadCompletedEventArgs e)
        {
            Assembly assembly = LoadAssemblyFromXap("XapContent.dll", e.Result);
            object loadedContent = assembly.CreateInstance("XapContent.Main");

            App app = (App)Application.Current;
            app.ApplicationVisualRoot.Children.Clear();
            app.ApplicationVisualRoot.Children.Add((UserControl)loadedContent);
        }

        private void wc_DownloadProgressChanged(Object sender, DownloadProgressChangedEventArgs e)
        {
            downloadProgress = e.ProgressPercentage;
            UpdateProgressBar();
        }

        private void UpdateProgressBar()
        {
            rectProgress.Width = rectStandard.ActualWidth * downloadProgress / 100;
            txtProgress.Text = "Loading ... " + downloadProgress.ToString() + "%";
        }

        private Assembly LoadAssemblyFromXap(string relativeUri, Stream xapPackageStream)
        {
            StreamResourceInfo xapPackageSri = new StreamResourceInfo(xapPackageStream, null);
            StreamResourceInfo assemblySri = Application.GetResourceStream(xapPackageSri,
                new Uri(relativeUri, UriKind.Relative));
            AssemblyPart assemblyPart = new AssemblyPart();

            return assemblyPart.Load(assemblySri.Stream);
        }

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
I have been working in the IT industry for some time. It is still exciting and I am still learning. I am a happy and honest person, and I want to be your friend.

Comments and Discussions