Every now and then I find I need to pull resources out of a Xap file. One of the issues with this is knowing what’s in the XAP. There have been a number of solutions I’ve used over the years (ok that is like 2.5 ish years) like having an index file either a Csv or Xml etc. A few weeks ago I ran across this little class that some guys were talking about on the Silverlight Insiders/MVP thread called un-zipper found here:
http://www.sharpgis.net/post/2009/04/21/REALLY-small-unzip-utility-for-Silverlight.aspx
What is cool about this class is it makes getting the assets out of a zap even if you don’t know what is in the xap up front easy and straight forward. From a using standpoint you basically need to create a webclient and a call and use the Unzipper to run through the contents. The Unzipper class deals with mucking up what is in the xap so all you need to-do is run through the collection and pull out what you need… Let us take a look at what you need to-do:
Assuming you have the Unzipper class (download here) you need the following libraries:
using System.Collections.ObjectModel;
using System.Net;
using System.Windows.Controls;
From here we need to create a web client
WebClient wc = new WebClient();
wc.OpenReadCompleted += new OpenReadCompletedEventHandler(wc_OpenReadCompleted);
wc.OpenReadAsync(new Uri("HackingXaps.xap", UriKind.RelativeOrAbsolute));
In this code we create the WebClient, add a handler and run the call pointed at our xap we want to load.
void wc_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
if (e.Error == null)
{
UnZipper unzip = new UnZipper(e.Result);
foreach (string filename in unzip.GetFileNamesInZip())
{
//do something with the file name…?
}
}
}
From here you can pretty much do whatever you want. Look to see this in the up coming version of the HackingSilverlight library.
Monday, 7 December 2009
Hacking the Silverlight Xap file
Posted on 15:51 by Unknown
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment