代码之家  ›  专栏  ›  技术社区  ›  Vague

StoreProduct IsInUserCollection始终为false

  •  1
  • Vague  · 技术社区  · 7 年前

    我们在微软商店里有一个UWP产品。该产品有许多订阅加载项。用户在应用程序内购买订阅加载项。 编辑 我们的代码是从微软文档中拼凑出来的 Enable subscription add-ons for your app

    StorePurchaseResult result = await product.RequestPurchaseAsync();
    if (result.Status == StorePurchaseStatus.Succeeded)
    

    结果返回 StorePurchaseStatus.Succeeded 是的。微软已将用户的钱用于订阅附加组件。到目前为止一切都很好。

    我们有这样的产品清单

    string[] productKinds = { "Durable" };
    List<String> filterList = new List<string>(productKinds);
    
    StoreProductQueryResult queryResult = await storeContext.GetAssociatedStoreProductsAsync(filterList);
    productList = queryResult.Products.Values.ToList();
    

    然后遍历

    foreach (StoreProduct storeProduct in products)
    {
        if (storeProduct.IsInUserCollection)
    ...
    }
    

    但是 storeProduct.IsInUserCollection 总是返回false。Microsoft已接受对该加载项的付款,但未将其添加到用户的产品集合中,因此我们无法验证他们是否已为该加载项付款。

    我们哪里出错了?

    编辑2 根据@lukeja的建议,我运行了这个方法

    async Task CheckSubsAsync()
    {
        StoreContext context = context = StoreContext.GetDefault();
        StoreAppLicense appLicense = await context.GetAppLicenseAsync();
    
        foreach (var addOnLicense in appLicense.AddOnLicenses)
        {
            StoreLicense license = addOnLicense.Value;
            Debug.WriteLine($"license.SkuStoreId {license.SkuStoreId}");
        }
    }
    

    这只输出一个附加组件。免费的附加组件。我们有16个附加组件,其中只有一个是免费的。

    为什么我们的付费附加组件订阅没有返回?

    编辑3 appLicense.AddOnLicenses 仅包括当前用户的加载项许可证,而不是应用程序的所有加载项。由@ Lukja提供的代码示例在订阅订阅的用户的上下文中运行时是预期的。

    1 回复  |  直到 7 年前
        1
  •  2
  •   lukeja    7 年前

    我不知道你为什么要用那种方法。我现在在我的应用程序和微软文档的方式是这样的…

    private async Task<bool> CheckIfUserHasSubscriptionAsync()
    {
        StoreAppLicense appLicense = await context.GetAppLicenseAsync();
    
        // Check if the customer has the rights to the subscription.
        foreach (var addOnLicense in appLicense.AddOnLicenses)
        {
            StoreLicense license = addOnLicense.Value;
    
            if (license.IsActive)
            {
                // The expiration date is available in the license.ExpirationDate property.
                return true;
            }
        }
    
        // The customer does not have a license to the subscription.
        return false;
    }