To dynamically load the second XAP file we have to deploy it along with the library assemblies into or around your ClientBin folder (in this case you'll be able to use relative path otherwise you'll have to specify an absolute path). Then we can use WebClient to download them on demand. So, here is a small simplified example on how to do it:
// This code should be placed where you want to download the dll
WebClient client = new WebClient();
client.OpenReadCompleted += OpenReadCompletedEventHandler(OnOpenReadCompleted);
client.OpenReadAsync(new Uri("ResourcesAndContent.xap", UriKind.Relative));
And OpenReadCompleted event handler :
void OnOpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
StreamResourceInfo sri = new StreamResourceInfo(e.Result, null);
StreamResourceInfo resourcesDll = Application.GetResourceStream(sri, new Uri("Resources.dll", UriKind.Relative));
AssemblyPart assmPart = new AssemblyPart();
Assembly assm = assmPart.Load(resourcesDll.Stream);
As you can see it's possible to load assemblies one by one, whenever required. There is a nice article by Eric Mork Dynamically Loading .Dlls in Silverlight and small sample of A Smaller XAP Preloader for Silverlight that provide more details on the method for dynamic loading of XAP file described above.There are also other ways to load libraries dynamically, e.g. using DeploymentCatalog in Managed Extensibility Framework (MEF). You can find explanation and a sample here Managed Extensibility Framework Overview .
Even more interesting things come into play when you have to use objects from a loaded assembly. The easiest (but not the most convenient and type safe) way is to use reflection to work with objects and types from a loaded library. So, couple samples on how it will look:
MyControl mc = (MyControl)assm.CreateInstance("Whatever.MyControl"); // Create an object
Type t = assm.GetType("Resources.Class1"); // Get type
string s = (string)t.InvokeMember("getData", BindingFlags.InvokeMethod, null, null, null); // Call static method
Using reflections has its downsides though. It's not as clear as regular coding and you loose benefits of strong typing that you have in regular C# or VB code. Good news that there is a way to use dynamic loading but still have benefits of strong typing and write code in a more readable format. There is a nice post from Jeff Prosise Cool Silverlight Trick #3 that explains in really deep details how to do so. Just to bring a general idea I will quote some of his main steps here:
1 . Add a reference to the library assembly to your Silverlight project, but change the “Copy Local” setting in the reference’s properties from true to false. This prevents Visual Studio from embedding the library assembly in the XAP file, but since the project contains a valid reference to the assembly, the compiler knows all about the types in it and will allow you to use them—even new them up.
2 . At run-time, use WebClient to download the assembly and AssemblyPart.Load to load it.
3 . Here’s the critical part. After loading the assembly, call a separate method to instantiate one of the assembly’s types, and decorate that method with a MethodImpl(MethodImplOptions.NoInlining) attribute. In addition, do not reference any of the types inside the library assembly in the method that loads the assembly. If you do, you’ll shoot yourself in the foot and what you’re trying to do will not work. Period.
So, using dynamically loading library is a really nice feature that helps to improve user experience with little extra work required.