Now that we have SilverlightDesktop working again we are now creating modules for it.
The first problem we ran into was that we had to remove the .dll from the .xap and place the .dll in the /Clientbin directory.
When you are constantly building your project and testing it, this gets old really fast. We realized we have to allow you to keep the assembly in the .xap and allow you to load it from there.
#region Load Assembly
void proxy_GetDesktopModuleCompleted(object sender, GetDesktopModuleCompletedEventArgs e)
{
objSilverlightDesktopModule = (SilverlightDesktopModule)e.Result;
strXAPName = objSilverlightDesktopModule.AssemblyName;
strAssemblyClassName = objSilverlightDesktopModule.ClassName;
StartDownloader();
}
private void StartDownloader()
{
WebClient downloader = new WebClient();
downloader.OpenReadCompleted += new OpenReadCompletedEventHandler(downloader_OpenReadCompleted);
downloader.OpenReadAsync(new Uri(strXAPName, UriKind.Relative));
}
void downloader_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
loadAssembly(e.Result);
}
private void loadAssembly(Stream s)
{
// from http://silverlight.net/forums/p/14941/49437.aspx (BlueAquarius)
// From http://joel.neubeck.net/2008/03/silverlight-how-to-on-demand-assembly-deployment/
try
{
Uri uri = new Uri(strXAPName.Replace(".xap", ".dll"), UriKind.Relative);
StreamResourceInfo xapPackageSri = new StreamResourceInfo(s, null);
StreamResourceInfo assemblySri = Application.GetResourceStream(xapPackageSri, uri);
AssemblyPart assemblyPart = new AssemblyPart();
Assembly a = assemblyPart.Load(assemblySri.Stream);
UserControl objUserControl = (UserControl)a.CreateInstance(strAssemblyClassName);
if (objUserControl == null)
{
debug("objUserControl is null");
return;
}
SilverlightWindowControl objSilverlightWindowControl =
new SilverlightWindowControl(objSilverlightDesktopModule.WindowSize);
objSilverlightWindowControl.Window.Content = objUserControl;
objSilverlightWindowControl.WindowName.Text = objSilverlightDesktopModule.ModuleName;
intWindowLocation = intWindowLocation + 10;
Canvas.SetLeft(objSilverlightWindowControl, (double)intWindowLocation);
Canvas.SetTop(objSilverlightWindowControl, (double)intWindowLocation);
this.LayoutRoot.Children.Add(objSilverlightWindowControl);
}
catch (Exception ex)
{
debug("Exception when loading the assembly part:");
debug(ex.ToString());
}
}
#endregion