获取应用和附加组件的产品信息

本文演示如何使用 Windows.Services.Store 命名空间中 StoreContext 类的方法访问当前应用或其某个加载项的应用商店相关信息。

有关完整的示例应用程序,请参阅 Store 示例

注释

Windows.Services.Store 命名空间是在 Windows 10 版本 1607 中引入的,并且只能在面向 Windows 10 周年版(10.0; 版本 14393) 或更高版本的项目中使用,需在 Visual Studio 中构建。 如果你的应用面向早期版本的 Windows 10,则必须使用 Windows.ApplicationModel.Store 命名空间而不是 Windows.Services.Store 命名空间。 有关详细信息,请参阅 本文

先决条件

这些示例具有以下先决条件:

  • 适用于面向 Windows 10 周年纪念版(10.0;版本 14393)或更高版本的通用 Windows 平台 (UWP) 应用的 Visual Studio 项目。
  • 在合作伙伴中心创建应用提交,此应用在应用商店中发布。 可以选择配置应用,以便在测试应用时无法在应用商店中发现它。 有关详细信息,请参阅我们的 测试指南
  • 如果要获取应用的加载项的产品信息,还必须 在合作伙伴中心创建加载项

这些示例中的代码假定:

  • 代码在 Page 上下文中运行,其中包含名为 workingProgressRing,以及名为 textBlock。 这些对象分别用于指示异步操作的发生和显示输出消息。
  • 代码文件中有一个 ,该文件使用 语句用于 Windows.Services.Store 命名空间.
  • 该应用是一个单用户应用,仅在启动应用的用户的上下文中运行。 有关详细信息,请参阅 应用内购买和试用版

注释

如果桌面应用程序使用 Desktop Bridge,则可能需要添加这些示例中未显示的其他代码来配置 StoreContext 对象。 有关详细信息,请参阅在使用桌面桥的桌面应用程序中使用的 StoreContext 类

获取当前应用的信息

若要获取有关当前应用的应用商店产品信息,请使用 GetStoreProductForCurrentAppAsync 方法。 这是一种异步方法,该方法返回可用于获取价格等信息的 StoreProduct 对象。

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

获取与当前应用关联的已知应用商店 ID 的加载项的信息

若要获取与当前应用关联的加载项的应用商店产品信息,并且已知道 应用商店 ID,请使用 GetStoreProductsAsync 方法。 这是一种异步方法,它返回表示每个加载项的 StoreProduct 对象的集合。 除了应用商店 ID,还必须将字符串列表传递给此方法,以标识加载项的类型。 有关支持的字符串值的列表,请参阅 ProductKind 属性。

注释

GetStoreProductsAsync 方法返回与应用关联的指定加载项的产品信息,而不考虑加载项当前是否可供购买。 若要检索当前可购买的当前应用的所有加载项的信息,请改用 GetAssociatedStoreProductsAsync 方法,如以下部分 中所述。

此示例使用与当前应用关联的指定应用商店 ID 检索持久加载项的信息。

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

获取可从当前应用购买的加载项的信息

若要获取当前可从当前应用购买的加载项的 Store 产品信息,请使用 GetAssociatedStoreProductsAsync 方法。 这是一种异步方法,它返回表示每个可用加载项的 StoreProduct 对象的集合。 必须将字符串列表传递给此方法,以标识要检索的加载项类型。 有关支持的字符串值的列表,请参阅 ProductKind 属性。

注释

如果应用程序有许多加载项可供购买,则可以使用 GetAssociatedStoreProductsWithPagingAsync 方法来利用分页功能返回加载项结果。

以下示例检索可以从当前应用程序购买的所有永久加载项、应用商店管理的消耗品加载项和开发者管理的消耗品加载项的信息。

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

获取用户已购买的当前应用的加载项信息

若要获取当前用户购买的加载项的 Store 产品信息,请使用 GetUserCollectionAsync 方法。 这是一种异步方法,它返回表示每个加载项的 StoreProduct 对象的集合。 必须将字符串列表传递给此方法,以标识要检索的加载项类型。 有关支持的字符串值的列表,请参阅 ProductKind 属性。

注释

如果应用有许多加载项,还可以使用 GetUserCollectionWithPagingAsync 方法通过分页返回加载项结果。

以下示例检索具有指定 应用商店 ID的耐用插件的信息。

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