65.9K
CodeProject 正在变化。 阅读更多。
Home

创建您自己的 WP7 部署应用程序

starIconstarIconstarIconstarIconstarIcon

5.00/5 (1投票)

2011年7月25日

Ms-PL

2分钟阅读

viewsIcon

15809

如何创建您自己的 WP7 部署应用程序

image

几天前,微软也向以色列开放了AppHub,我开始为WP7编写更多的游戏和应用程序(WP7提交应用程序 - 入口程序集缺少[NeutralResourceLanguage]属性)。

在开发几个应用程序时,我注意到在Visual Studio 2010中部署到我的WP7设备之前,我想查看应用程序的属性。从Visual Studio本身部署项目很容易,但我也使用许多在Visual Studio之外的XAP文件,默认的应用程序部署并没有给我想要的结果。

如何构建你自己的WP7部署应用程序

在开始讨论如何将XAP文件部署到WP7应用程序之前?
有一个名为InstallApplication的方法,它获取应用程序指南、应用程序的类型、应用程序图标的路径以及Xap文件本身。

public RemoteApplication InstallApplication(Guid productId, 
Guid instanceId, string applicationGenre, string iconPath,
 string xapPackage);

首先,如何获取所有连接到你机器的WP7设备?

将“Microsoft.SmartDevice.Connectivity.dll”添加到你的项目中
(C:\Windows\Microsoft.Net\assembly\GAC_MSIL\Microsoft.SmartDevice.Connectivity\
v4.0_10.0.0.0__b03f5f7f11d50a3a\Microsoft.SmartDevice.Connectivity.dll
)

然后我使用DatastoreManager来获取所有平台,并为每个平台获取设备。

image

static IEnumerable<object> GetDevices()
{
    var manager = new DatastoreManager(CultureInfo.CurrentUICulture.LCID);
    return manager.GetPlatforms().SelectMany(platform => 
      platform.GetDevices()).Cast<object>().ToArray();
}

每个XAP文件(基本上是zip文件)都包含“WMAppManifest.xml”,其中包含所有应用程序信息。

<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">

现在我已在我的窗口上启用了拖放功能,以接受XAP文件并从XAP内部的“WMAppManifest.xml”文件中提取我想要的信息。

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;
}

因此,我创建了一个XapInfo类来包含所有部署数据。

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;
}

XapInfo类中,我又进行了一次zip操作来提取应用程序图标。

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;
}

现在当你拥有XAP文件的所有信息后,就可以将应用程序安装到你的设备上。下面的方法也会确保如果应用程序已经安装,则先卸载它,然后再执行新的安装。

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);
}
© . All rights reserved.