获取应用和加载项的产品信息Get product info for apps and add-ons

本文介绍了如何使用 Windows.Services.Store 命名空间中 StoreContext 类的方法,来访问当前应用或它的一个加载项的与 Microsoft Store 相关的信息。This article demonstrates how to use methods of the StoreContext class in the Windows.Services.Store namespace to access Store-related info for the current app or one of its add-ons.

有关完整的应用程序示例,请参阅Microsoft Store 示例For a complete sample application, see the Store sample.

备注

Windows.Services.Store 命名空间在 Windows 10 版本 1607 中引入,它仅可用于面向 Windows 10 周年纪念版(10.0;版本 14393)或 Visual Studio 更高版本的项目中。The Windows.Services.Store namespace was introduced in Windows 10, version 1607, and it can only be used in projects that target Windows 10 Anniversary Edition (10.0; Build 14393) or a later release in Visual Studio. 如果你的应用面向 Windows 10 的较早版本,则必须使用 Windows.ApplicationModel.Store 命名空间来替代 Windows.Services.Store 命名空间。If your app targets an earlier version of Windows 10, you must use the Windows.ApplicationModel.Store namespace instead of the Windows.Services.Store namespace. 有关详细信息,请参阅此文For more information, see this article.

先决条件Prerequisites

这些示例有以下先决条件:These examples have the following prerequisites:

  • 适用于面向 Windows 10 周年纪念版(10.0;版本 14393)或更高版本的通用 Windows 平台 (UWP) 应用的 Visual Studio 项目。A Visual Studio project for a Universal Windows Platform (UWP) app that targets Windows 10 Anniversary Edition (10.0; Build 14393) or a later release.
  • 你有合作伙伴中心中的创建应用提交并在应用商店中发布此应用。You have created an app submission in Partner Center and this app is published in the Store. 在测试应用期间,你可以选择将应用配置为在应用商店中隐藏。You can optionally configure the app so it is not discoverable in the Store while you test it. 有关详细信息,请参阅我们的测试指南For more information, see our testing guidance.
  • 如果你想要获取应用的加载项的产品信息,还必须创建合作伙伴中心中的加载项If you want to get product info for an add-on for the app, you must also create the add-on in Partner Center.

这些示例中的代码假设:The code in these examples assume:

  • 代码在含有 ProgressRing(名为 workingProgressRing)和 TextBlock(名为 textBlock)的 Page 上下文中运行。The code runs in the context of a Page that contains a ProgressRing named workingProgressRing and a TextBlock named textBlock. 这些对象分别用于指示是否正在进行异步操作和显示输出消息。These objects are used to indicate that an asynchronous operation is occurring and to display output messages, respectively.
  • 代码文件有一个适用于 Windows.Services.Store 命名空间的 using 语句。The code file has a using statement for the Windows.Services.Store namespace.
  • 该应用是单用户应用,仅在启动该应用的用户上下文中运行。The app is a single-user app that runs only in the context of the user that launched the app. 有关详细信息,请参阅应用内购买和试用For more information, see In-app purchases and trials.

备注

如果你有使用桌面桥的桌面应用程序,可能需要添加未在这些示例中显示的额外代码来配置 StoreContext 对象。If you have a desktop application that uses the Desktop Bridge, you may need to add additional code not shown in these examples to configure the StoreContext object. 有关详细信息,请参阅在使用桌面桥的桌面应用程序中使用 StoreContext 类For more information, see Using the StoreContext class in a desktop application that uses the Desktop Bridge.

获取当前应用的信息Get info for the current app

若要获取当前应用的应用商店产品信息,请使用 GetStoreProductForCurrentAppAsync 方法。To get Store product info about the current app, use the GetStoreProductForCurrentAppAsync method. 这是一种异步方法,它返回 StoreProduct 对象,该对象可用于获取价格等信息。This is an asynchronous method that returns a StoreProduct object that you can use to get info such as the price.

private StoreContext context = null;

public async void GetAppInfo()
{
    if (context == null)
    {
        context = StoreContext.GetDefault();
        // If your app is a desktop app that uses the Desktop Bridge, you
        // may need additional code to configure the StoreContext object.
        // For more info, see https://aka.ms/storecontext-for-desktop.
    }

    // Get app store product details. Because this might take several moments,   
    // display a ProgressRing during the operation.
    workingProgressRing.IsActive = true;
    StoreProductResult queryResult = await context.GetStoreProductForCurrentAppAsync();
    workingProgressRing.IsActive = false;

    if (queryResult.Product == null)
    {
        // The Store catalog returned an unexpected result.
        textBlock.Text = "Something went wrong, and the product was not returned.";

        // Show additional error info if it is available.
        if (queryResult.ExtendedError != null)
        {
            textBlock.Text += $"\nExtendedError: {queryResult.ExtendedError.Message}";
        }

        return;
    }

    // Display the price of the app.
    textBlock.Text = $"The price of this app is: {queryResult.Product.Price.FormattedBasePrice}";
}

通过与当前应用关联的已知 Store ID 获取加载项的信息Get info for add-ons with known Store IDs that are associated with the current app

若要获取你已知道其 Store ID 的当前应用关联加载项的 Microsoft Store 产品信息,请使用 GetStoreProductsAsync 方法。To get Store product info for add-ons that are associated with the current app and for which you already know the Store IDs, use the GetStoreProductsAsync method. 这是一种异步方法,可返回一组代表每一个加载项的 StoreProduct 对象。This is an asynchronous method that returns a collection of StoreProduct objects that represent each of the add-ons. 除了 Store ID 之外,还必须向此方法传递一列字符串,用于标识加载项的类型。In addition to the Store IDs, you must pass a list of strings to this method that identify the types of the add-ons. 有关受支持的字符串值列表,请参阅 ProductKind 属性。For a list of the supported string values, see the ProductKind property.

备注

GetStoreProductsAsync 方法返回与应用关联的指定加载项的产品信息,无论加载项当前是否可购买。The GetStoreProductsAsync method returns product info for the specified add-ons that are associated with the app, regardless of whether the add-ons are currently available for purchase. 若要检索当前可购买的当前应用的所有加载项的信息,请改用 GetAssociatedStoreProductsAsync 方法,如下一部分所述。To retrieve info for all the add-ons for the current app that can currently be purchased, use the GetAssociatedStoreProductsAsync method as described in the following section instead.

此示例使用与当前应用关联的指定 Store ID 检索持久型加载项的信息。This example retrieves info for durable add-ons with the specified Store IDs that are associated with the current app.

private StoreContext context = null;

public async void GetProductInfo()
{
    if (context == null)
    {
        context = StoreContext.GetDefault();
        // If your app is a desktop app that uses the Desktop Bridge, you
        // may need additional code to configure the StoreContext object.
        // For more info, see https://aka.ms/storecontext-for-desktop.
    }

    // Specify the kinds of add-ons to retrieve.
    string[] productKinds = { "Durable" };
    List<String> filterList = new List<string>(productKinds);

    // Specify the Store IDs of the products to retrieve.
    string[] storeIds = new string[] { "9NBLGGH4TNMP", "9NBLGGH4TNMN" };

    workingProgressRing.IsActive = true;
    StoreProductQueryResult queryResult =
        await context.GetStoreProductsAsync(filterList, storeIds);
    workingProgressRing.IsActive = false;

    if (queryResult.ExtendedError != null)
    {
        // The user may be offline or there might be some other server failure.
        textBlock.Text = $"ExtendedError: {queryResult.ExtendedError.Message}";
        return;
    }

    foreach (KeyValuePair<string, StoreProduct> item in queryResult.Products)
    {
        // Access the Store info for the product.
        StoreProduct product = item.Value;

        // Use members of the product object to access info for the product...
    }
}

获取可从当前应用购买的加载项的信息Get info for add-ons that are available for purchase from the current app

若要获取目前可从当前应用购买的加载项的 Microsoft Store 产品信息,请使用 GetAssociatedStoreProductsAsync 方法。To get Store product info for the add-ons that are currently available for purchase from the current app, use the GetAssociatedStoreProductsAsync method. 这是一种异步方法,可返回一组代表每一个可用加载项的 StoreProduct 对象。This is an asynchronous method that returns a collection of StoreProduct objects that represent each of the available add-ons. 必须向此方法传递一列字符串,用于标识你想要检索的加载项的类型。You must pass a list of strings to this method that identify the types of add-ons you want to retrieve. 有关受支持的字符串值的列表,请参阅 ProductKind 属性。For a list of the supported string values, see the ProductKind property.

备注

如果应用有许多加载项可供购买,你也可以使用 GetAssociatedStoreProductsWithPagingAsync 方法分页返回加载项结果。If the app has many add-ons that are available for purchase, you can alternatively use the GetAssociatedStoreProductsWithPagingAsync method to use paging to return the add-on results.

以下示例检索可从当前应用购买的所有持久型加载项、Microsoft Store 管理的易耗型加载项和开发人员管理的可消耗加载项的信息。The following example retrieves info for all durable add-ons, Store-managed consumable add-ons, and developer-managed consumable add-ons that are available for purchase from the current app.

private StoreContext context = null;

public async void GetAddOnInfo()
{
    if (context == null)
    {
        context = StoreContext.GetDefault();
        // If your app is a desktop app that uses the Desktop Bridge, you
        // may need additional code to configure the StoreContext object.
        // For more info, see https://aka.ms/storecontext-for-desktop.
    }

    // Specify the kinds of add-ons to retrieve.
    string[] productKinds = { "Durable", "Consumable", "UnmanagedConsumable" };
    List<String> filterList = new List<string>(productKinds);

    workingProgressRing.IsActive = true;
    StoreProductQueryResult queryResult = await context.GetAssociatedStoreProductsAsync(filterList);
    workingProgressRing.IsActive = false;

    if (queryResult.ExtendedError != null)
    {
        // The user may be offline or there might be some other server failure.
        textBlock.Text = $"ExtendedError: {queryResult.ExtendedError.Message}";
        return;
    }

    foreach (KeyValuePair<string, StoreProduct> item in queryResult.Products)
    {
        // Access the Store product info for the add-on.
        StoreProduct product = item.Value;

        // Use members of the product object to access listing info for the add-on...
    }
}

获取用户购买的当前应用的加载项的信息Get info for add-ons for the current app that the user has purchased

若要获取当前用户购买的加载项的 Microsoft Store 产品信息,请使用 GetUserCollectionAsync 方法。To get Store product info for add-ons that the current user has purchased, use the GetUserCollectionAsync method. 这是一种异步方法,可返回代表每一个加载项的 StoreProduct 对象的集合。This is an asynchronous method that returns a collection of StoreProduct objects that represent each of the add-ons. 必须向此方法传递一列字符串,用于标识你想要检索的加载项的类型。You must pass a list of strings to this method that identify the types of add-ons you want to retrieve. 有关受支持的字符串值列表,请参阅 ProductKind 属性。For a list of the supported string values, see the ProductKind property.

备注

如果应用具有许多加载项,你也可以使用 GetUserCollectionWithPagingAsync 方法分页返回加载项结果。If the app has many add-ons, you can alternatively use the GetUserCollectionWithPagingAsync method to use paging to return the add-on results.

以下示例使用指定的 Store ID 检索持久型加载项的信息。The following example retrieves info for durable add-ons with the specified Store IDs.

private StoreContext context = null;

public async void GetUserCollection()
{
    if (context == null)
    {
        context = StoreContext.GetDefault();
        // If your app is a desktop app that uses the Desktop Bridge, you
        // may need additional code to configure the StoreContext object.
        // For more info, see https://aka.ms/storecontext-for-desktop.
    }

    // Specify the kinds of add-ons to retrieve.
    string[] productKinds = { "Durable" };
    List<String> filterList = new List<string>(productKinds);

    workingProgressRing.IsActive = true;
    StoreProductQueryResult queryResult = await context.GetUserCollectionAsync(filterList);
    workingProgressRing.IsActive = false;

    if (queryResult.ExtendedError != null)
    {
        // The user may be offline or there might be some other server failure.
        textBlock.Text = $"ExtendedError: {queryResult.ExtendedError.Message}";
        return;
    }

    foreach (KeyValuePair<string, StoreProduct> item in queryResult.Products)
    {
        StoreProduct product = item.Value;

        // Use members of the product object to access info for the product...
    }
}