Windows Store In App Purchase 问题“价值不在预期范围内”

Windows Store In App Purchase Issue “Value does not fall within the expected range”

当使用命名空间 Windows.services.Store 中的 StoreContext.RequestPurchaseAsync() 从 WPF 桌面应用程序执行应用内购买时,我们得到一个 StorePurchaseResult扩展错误消息 “值不在预期范围内。”

我们的应用程序已发布,可从 Windows 商店下载。

它是使用 DesktopAppConverter 工具转换的。我们根据Store中的描述设置manifest.appx(Identity Name, Publisher...).

我们按照下面提供的说明在使用 Desktop Bridge 的 Windows 桌面应用程序的 UI 线程中使用 C# 进行应用内购买。

https://docs.microsoft.com/en-us/windows/uwp/monetize/in-app-purchases-and-trials https://docs.microsoft.com/en-us/windows/uwp/monetize/enable-in-app-purchases-of-apps-and-add-ons

在我们应用程序的代码中,我们声明了 IInitializeWithWindow 接口:

[ComImport]
[Guid("3E68D4BD-7135-4D10-8018-9FB6D9F33FA1")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IInitializeWithWindow
{
  void Initialize(IntPtr hwnd);
}

然后,当我们的应用程序启动时,我们使用单用户方式获取 StoreContext(存储在 storeContext_ 属性中):

// Init the store context
storeContext_ = StoreContext.GetDefault();
IInitializeWithWindow initWindow = (IInitializeWithWindow)(object)storeContext_;
initWindow.Initialize(System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle);

在此之后,我们设法检索了 StoreLicense(存储在 storeLicense_ 属性中)并列出了关联的商店产品,没有任何错误

// Get the current user license
storeLicense_ = await storeContext_.GetAppLicenseAsync();
if (storeLicense_ == null)
{
    MessageBox.Show("An error occurred while retrieving the license.", "StoreLicenseApp Error");
    return;
}

// Create a filtered list of the product AddOns I care about
string[] filterList = new string[] { "Durable" };

// Get list of Add Ons this app can sell, filtering for the types we know about
StoreProductQueryResult addOns = await storeContext_.GetAssociatedStoreProductsAsync(filterList);
if (addOns.ExtendedError != null)
{
    MessageBox.Show("Impossible to retreive the list of the products on the store.\n" + addOns.ExtendedError.Message,
                               "Get Associated Store Products Error");
}
            

从商店检索到产品的商店 ID 后,我们等待用户单击调用下面回调的购买按钮。

private async void PurchaseButton_Clicked(object sender, RoutedEventArgs e)
{
   StorePurchaseResult result = await storeContext_.RequestPurchaseAsync(selectedStoreId);
   
   // Capture the error message for the operation, if any.
   string extendedError = string.Empty;
   if (result.ExtendedError != null)
   {
                   extendedError = result.ExtendedError.Message;
   }
               
               [...]
}

RequestPurchaseAsync() return 一个错误,而不是显示用于购买产品的 Metro 界面。 这是扩展错误 returned :

Message = "Value does not fall within the expected range."

HResult = -2147024809

关于如何解决此问题的任何线索?

这是使用 WSCOLLECT.exe 收集的应用内购买任务类别的错误日志:

1) 调用消息 Windows::Services::Store::StoreContext::RequestPurchaseAsync(9PMRHM0MJ85K)。 (CV:+hcSKxR9+UGX7BQ/.1.3) 函数 Windows::Services::Store::StoreContext::RequestPurchaseAsync 错误代码 -1 来源\storecontext.cpp 行号 1194

2) 消息:ChkHr(hrGetTicket) 功能:Windows::Services::Store::PurchaseOperation::_PromptForPasswordAndEnterOrderWithRetry 错误代码:-2147024809 来源:\purchaseoperation.cpp 行号 506

3) 消息:ChkHr(_PromptForPasswordAndEnterOrderWithRetry(fPromptForCredentials)) 功能:Windows::Services::Store::PurchaseOperation::_ProceedToPurchase 错误代码:-2147024809 来源:\purchaseoperation.cpp 行号:630

4) 消息:ChkHr(_ProceedToPurchase(needsToSignIn)) 功能:Windows::Services::Store::PurchaseOperation::_Purchase 错误代码:-2147024809 来源:\purchaseoperation.cpp 行号:792

5) 消息 ChkHr(_hresultOfOperation) 函数 Windows::Services::Store::RequestPurchaseOperation::DoWork 错误代码 -2147024809 来源\请求purchaseoperation.cpp 行号 115

在我们的 PurchaseButton_Clicked() 函数中调用的 RequestPurchaseAsync() 返回错误,因为我们在应用程序启动时在另一个函数中初始化存储上下文。

我们通过在 PurchaseButton_Clicked() 中放置存储上下文初始化指令解决了这个问题,如下所示:

public async void PurchaseSelectedProduct(ProductModel product){
    if (product == null){
        MessageBox.Show("Product not valid", "PurchaseInApp Error");
        return;
    }

    // Init store context for purchase
    IInitializeWithWindow initWindow = (IInitializeWithWindow)(object)storeContext_;
    initWindow.Initialize(System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle);

    // Buy through in app purchase sdk
    StorePurchaseResult result = null;
    try { result = await product.StoreProduct.RequestPurchaseAsync(); }
}