Thursday, September 9, 2010

How To : Load Multiple XAP files in Silverlight Application

Currently i am working on a project where i had a requirement to load multiple xap files in a silverlight application, or let me keep this way i need to load xap files OnDemand.
I was going through this article on msdn where it explains about "how to load assemblies on demand", before reading this article i really had no idea of how to load multiple xap files, but now i have done that yipee... :)
I just want to blog a bit about how to load multiple xap files in silvelright application, and also share some sample application which demonstrates this. As usual you can find the source code the end of this blog post.

To get started with this sample i've created a silverlight project and added two silverlight class libraries, now my solution looks as shown below

















First let us check out how to load a single xap file, the below code shows how to read a xap file and read the dll file for the perticular class for which we want to create the instance and returns that perticular instance.
The Things to check out in the below code snippet are the two private variables:
  • applicationName - The name of the xap file with out extension.
  • control - The class in the above mentioned application for which you want to create instance.
public partial class MainPage : UserControl
    {
        string applicationName = "SilverlightApplication1";
        string control = "Control";

        public MainPage()
        {
            InitializeComponent();
            WebClient wc = new WebClient();
            wc.OpenReadCompleted += new OpenReadCompletedEventHandler(wc_OpenReadCompleted);
            wc.OpenReadAsync(new Uri("SilverlightApplication1.xap", UriKind.Relative));
        }

        void wc_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            StreamResourceInfo resourceInfoDLL = Application.GetResourceStream(new StreamResourceInfo(e.Result, null),
                                                 new Uri(string.Format("{0}.dll", applicationName), UriKind.Relative));
            AssemblyPart assemblyPart = new AssemblyPart();
            Assembly assembly = assemblyPart.Load(resourceInfoDLL.Stream);
            UIElement element = assembly.CreateInstance(string.Format("{0}.{1}", applicationName, control)) as UIElement;
            LayoutRoot.Children.Add(element);
        }
    }
Using the webclient we can read the xap file and load its content in the silverlight applicaiton as shown above, I have added the "Control" instance, which is a user control in SilverlightApplication1 project to the LayoutRoot, which is a grid in our Main Silverlight application.
If we want to load multiple xaps then we need to create multiple instances of webclient and load xap files, The final output of the shared source code would look like the below image.

At last here is the Source Code for this particular blog post, to load xap files ondemand.
Hope this post helps some one in need... :)

2 comments:

  1. Nice about this coding . thanks for sharing all about Silverlight.

    ReplyDelete
  2. Hi Kranthi,

    Thanks a lot for this post. I just have one question - is it possible to pass any parameters to that element you're displaying?

    ReplyDelete