Dynamically load an .xap into an silverlight app (2)
November 12, 2009 at 5:41 pm | Posted in 27372874 | Leave a commentVisual studio solution, Live demo
(The code used in XapLoader class is explained in an earlier blog entry)
The XapLoader class is based on the class of pbrooks. I have added preloading support and minimized the .xap size.
this.Stream = e.Result; // Host content in a contentPresenter this.cpHost.Content = instance;
Dynamically load an .xap into a silverlight app
November 7, 2009 at 5:14 pm | Posted in Other | Leave a commentVisual studio solution, Live demo
(Update: You can also use XapLoader, a helper class for loading .xap files into silverlight apps)
This article shows how to dynamically load an .xap into a silverlight app.
Using this technique you can use create a small (< 10 KB) preloader application which shows a loading bar or some fancy animations while the user waits for the big main app.
Furthermore it enables you to divide a big silverlight app into some small ones which are dynamically loaded when they are needed.
The loading of the .xap file is done in two steps. First we download the .xap file from the server (some standard webclient stuff).
// "?t=" + DateTime.Now.Ticks.ToString() to prevent caching by browser
this.Uri = new Uri("ImagePresenter.xap?t=" + DateTime.Now.Ticks.ToString(), UriKind.RelativeOrAbsolute);
this.RootAssembly = "ImagePresenter.dll";
this.TypeName = "ImagePresenter.MainPage";
// Use WebClient to download the .xap file (and get the stream)
WebClient wc = new WebClient();
wc.DownloadProgressChanged += new DownloadProgressChangedEventHandler(wc_DownloadProgressChanged);
wc.OpenReadCompleted += new OpenReadCompletedEventHandler(wc_OpenReadCompleted);
wc.OpenReadAsync(this.Uri);
Then we use the following code to start the app.
this.Stream = e.Result;
// Get the stream of the manifest (which lists the dlls included in the .xap)
// see http://blogs.msdn.com/katriend/archive/2008/03/16/silverlight-2-structure-of-the-new-xap-file-silverlight-packaged-application.aspx
StreamResourceInfo manifestResourceInfo = Application.GetResourceStream(
new StreamResourceInfo(this.Stream, null),
new Uri("AppManifest.xaml", UriKind.Relative));
XmlReader reader = XmlReader.Create(manifestResourceInfo.Stream);
Assembly asm = null;
StreamResourceInfo assemblyStream = new StreamResourceInfo(this.Stream, "application/binary");
// Step through all .dlls
while (reader.Read())
{
// If it's an assembly part...
if (reader.IsStartElement("AssemblyPart"))
{
reader.MoveToAttribute("Source");
reader.ReadAttributeValue();
// ...get the stream
StreamResourceInfo si = Application.GetResourceStream(assemblyStream, new Uri(reader.Value, UriKind.Relative));
AssemblyPart p = new AssemblyPart();
// If it's the main assembly,...
if (reader.Value == this.RootAssembly)
{
// ...load it and remember it for later use
asm = p.Load(si.Stream);
}
else
{
// Otherwise just load it
p.Load(si.Stream);
}
}
}
if (asm == null)
{
throw new InvalidOperationException("Could not find specified assembly.");
}
UIElement instance = asm.CreateInstance(this.TypeName) as UIElement;
if (instance == null)
{
throw new InvalidOperationException("Could not create instance of requested type.");
}
// Host content in a contentPresenter
this.cpHost.Content = instance;
Project started: Yune Web OS
November 7, 2009 at 2:08 pm | Posted in Yune web OS Dev Log | 1 CommentTags: silverlight, web OS
About a week ago I have started a new silverlight project named “Yune”. It should be a web OS just like Live Mesh, g.ho.st, iCloud or eyeOS.
The main structure is simple. Yune uses a silverlight client application which is connected via RIA services with an ASP.NET server (The so-called cloud).
During the past days I have set up a basic Feature Listing and began writing the first code. Fortunately, I could use some code from older project, so that the basic window and application management is already done.
The above image shows the screen of the Yune web OS. Two integrated apps, the text editor and the Yune explorer are currently running.
The apps were packaged as external .xap files on the server and dynamically downloaded and executed when I started them using the task bar.
Blog at WordPress.com. | Theme: Pool by Borja Fernandez.
Entries and comments feeds.

