When you build your silverlight application, there arises a point where your xap is a couple of MB(s) in size. To improve your applications download experience you may decide to split your application into different modules.
In this regard, I would advise you to keep only one application with its output as a xap, all the rest should be class library projects.
In this post I will explain the method of loading such dynamic modules into your application. For this sample, I have used three projects namely,
- SampleApp as the main navigation project,
- SampleModule as the dynamic assembly,
- SampleApp.Web as the host for SampleApp
At first, create a sample control in the SampleModule project, lets call it SilverlightControl1. Add a simple text block with some static text.
<Grid x:Name=”LayoutRoot” Background=”#EEEEEE”>
<TextBlock Text=”Sample Control in Sample Module” FontSize=”13″ VerticalAlignment=”Center” HorizontalAlignment=”Center”/>
Now, build your SampleModule and add a zip of the SampleModule.dll in the ClientBin folder in the Host project.
In the SampleApp project, open the Home.xaml.cs file.(I am assuming that you have used the default template for a Silverlight Navigation Application provided by Visual Studio).
We would now add come code to the OnNavigatedTo method which will load our dynamic module.
protected override void OnNavigatedTo(NavigationEventArgs e)
if (App.Assemblies.Any(a => a.FullName.StartsWith(“SampleModule”)))
var module = App.Assemblies.FirstOrDefault(a => a.FullName.StartsWith(“SampleModule”));
PageScrollViewer.Content = module.CreateInstance(“SampleModule.SilverlightControl1”);
WebClient client = new WebClient();
client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);
client.OpenReadAsync(new Uri(“SampleModule.zip”, UriKind.Relative));
void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
var resourceInfo = Application.GetResourceStream(new StreamResourceInfo(e.Result, “application/x-zip-compressed”), new Uri(“SampleModule.dll”, UriKind.Relative));
AssemblyPart asmPart = new AssemblyPart();
Assembly asm = asmPart.Load(resourceInfo.Stream);
PageScrollViewer.Content = asm.CreateInstance(“SampleModule.SilverlightControl1”);
The above code performs two basic tasks, at first it loads your module using a Web Client and then creates the instance of your control using the module it has already downloaded.I also copy the downloaded assembly in a global assemblies list, so that it can be referenced later when required. Here I have hard coded the module and controls names, you can load by any configuration method available to you.
This code is a basic skeleton and should be used as a reference purpose. You may need to add some error handling and show a delay to the user while the module is being downloaded.
Further Improvements can be made to this code so that it is clean and modular. The first being, the code needs to be ported to a class which takes certain arguments like assembly name, source url, Control type name etc. Then the class can also save the assembly in isolated storage, instead of it being stored in memory.
I would provide more insights into this topic in future posts.
For more on this topic see Dynamic loading of Assembles II
- Cached Assemblies in Silverlight
- Assembly Part
- How to: Create a New Silverlight Project
Complete Source Code : DynamicXap.zip