Couple of days ago, Microsoft opened AppHub for Israel as well, I’ve started to write more games and applications for WP7 (WP7 Submit Application - The [NeutralResourceLanguage] attribute is missing on the entry assembly).
While working on several applications, I noticed that I want to see the application properties before deploying them inside my WP7 device, when working in Visual Studio 2010. It’s easy because you can deploy your project from Visual Studio himself but I’m also working with many XAP files outside of Visual Studio and the default application deployment didn’t give me what I wanted.
How To Build Your Own WP7 Deployment Application
Before we get started with how to deploy XAP file to WP7 Application?
There is a method called InstallApplication
that gets the application guide, the genre of the application, the path for the Application Icon and the Xap file itself.
public RemoteApplication InstallApplication(Guid productId,
Guid instanceId, string applicationGenre, string iconPath,
string xapPackage);
First, how to get all WP7 devices connected to your machine?
Add “Microsoft.SmartDevice.Connectivity.dll” to your project
(C:\Windows\Microsoft.Net\assembly\GAC_MSIL\Microsoft.SmartDevice.Connectivity\
v4.0_10.0.0.0__b03f5f7f11d50a3a\Microsoft.SmartDevice.Connectivity.dll)
Then I used the DatastoreManager
to obtain all Platforms and for each platform, get the devices.
static IEnumerable<object> GetDevices()
{
var manager = new DatastoreManager(CultureInfo.CurrentUICulture.LCID);
return manager.GetPlatforms().SelectMany(platform =>
platform.GetDevices()).Cast<object>().ToArray();
}
Each XAP file (basically zip file) contains “WMAppManifest.xml” with all the application information inside.
<App xmlns="" ProductID="{GUID}" Title="Raise My Dog"
RuntimeType="Silverlight" Version="1.0.0.0" Genre="apps.normal"
Author="Shai Raiten" Description="Raise My Dog is an interactive game
for WP7 that allows you to raise a dog inside you mobile device."
Publisher="RaisePets">
Now I’ve enabled Drag&Drop on my window to accept XAP files and extract the information I wanted from the “WMAppManifest.xml” file located inside the XAP.
private const string FileFilter = "WMAppManifest.xml";
private XapInfo GetXapInformation(string xapPath)
{
try
{
var fastZip = new FastZip();
var tempFile = Path.Combine(Path.GetTempPath(),
Path.GetRandomFileName());
fastZip.ExtractZip(xapPath, tempFile, FileFilter);
var files = Directory.GetFiles(tempFile);
if (files.Length == 0) return null;
using (Stream stream2 = File.OpenRead(files[0]))
{
var document = new XPathDocument(stream2);
var selectSingleNode = document.CreateNavigator()
.SelectSingleNode("//App");
if (selectSingleNode != null)
{
return new XapInfo(selectSingleNode, files[0], xapPath);
}
}
}
catch
{
}
return null;
}
So I’ve create XapInfo
class to contain the entire data for the deployment.
public XapInfo(XPathNavigator node, string filePath, string xapFile)
{
this.Guid = new Guid(node.GetAttribute("ProductID", string.Empty));
this.Title = node.GetAttribute("Title", string.Empty);
this.Description = node.GetAttribute("Description", string.Empty);
this.Version = node.GetAttribute("Version", string.Empty);
this.Author = node.GetAttribute("Author", string.Empty);
this.Publisher = node.GetAttribute("Publisher", string.Empty);
this.IconPath = GetXapIcon(xapFile);
this.XapFilePath = xapFile;
}
Inside the XapInfo
class, I had another zip operation to extract the Application Icon.
private const string IconFilter = "ApplicationIcon.png";
private string GetXapIcon(string xapPath)
{
string iconPath;
try
{
var fastZip = new FastZip();
var tempFile = Path.Combine(Path.GetTempPath(),
Path.GetRandomFileName());
fastZip.ExtractZip(xapPath, tempFile, IconFilter);
var files = Directory.GetFiles(tempFile);
if (files.Length == 0) return null;
var fileStream = File.OpenRead(files[0]) ??
Assembly.GetExecutingAssembly().
GetManifestResourceStream(IconFilter);
var tempFileName = Path.GetTempFileName();
using (var stream3 = new FileStream(tempFileName,
FileMode.Create))
{
fileStream.CopyTo(stream3);
}
iconPath = tempFileName;
}
catch (Exception)
{
iconPath = null;
}
return iconPath;
}
Now when you have all the information for the XAP file, you can install the application on your device. The below method will also make sure that if the application is already installed, then Uninstall it and then perform the new installation.
var device = (Device) e.Argument;
try
{
device.Connect();
if (device.IsApplicationInstalled(_xapInfo.Guid.Value))
{
device.GetApplication(_xapInfo.Guid.Value).Uninstall();
}
device.InstallApplication(_xapInfo.Guid.Value, _xapInfo.Guid.Value,
"NormalApp", _xapInfo.IconPath, _xapInfo.XapFilePath);
device.Disconnect();
}
catch (SmartDeviceException ex)
{
MessageBox.Show(ex.Message, "Deploy Application", MessageBoxButton.OK,
MessageBoxImage.Information);
}