如何使用.NET代码从Windows Phone Marketplace获取我的应用程序的深层链接?

时间:2012-11-30 05:27:23

标签: windows-phone

如何从Windows Phone Marketplace以编程方式获取我的应用程序的深层链接,以便我可以在我的代码中使用它?

1 个答案:

答案 0 :(得分:10)

获取AppDeeplink非常有用,例如在ShareStatusTask和ShareLinkTask中 但是,您可以使用一些非平凡的代码从应用程序清单文件中获取真正的AppID。这就是我的所作所为:
首先在资源的某处保存Windows Phone应用程序深层链接的字符串,这很简单:

  

“http://windowsphone.com/s?appId={0}”

然后你必须通过打开App Manifest文件并找到正确的标签来找到真正的AppId,我在MarketplaceHelper中使用此代码来执行此操作:

static MarketplaceHelper()
{
    try
    {
        // load product details from WMAppManifest.xml
        XElement app = XElement.Load("WMAppManifest.xml").Descendants("App").Single();

        Title = GetValue(app, "Title");
        Version = new Version(GetValue(app, "Version"));
        Author = GetValue(app, "Author");
        Publisher = GetValue(app, "Publisher");
        Description = GetValue(app, "Description");

        // remove the surrounding braces
        string productID = GetValue(app, "ProductID");
        ProductID = Regex.Match(productID, "(?<={).*(?=})").Value;
    }
    catch (Exception e)
    {
        // should not happen, every application has this field and should containt the ProductID and Version
    }
}

private static string GetValue(XElement app, string attrName)
{
    XAttribute at = app.Attribute(attrName);
    return at != null ? at.Value : null;
}

如果Manifest在那里并且格式正确,它应该是,否则应用程序将无法运行,您可以通过这种方式获得您想要的任何数据。

现在您可以像这样构建深层链接:

string deeplink = string.Format(AppResources.DeepLinkFormat, MarketplaceHelper.ProductID);