c# - Windows Store In App Purchase问题 “Value does not fall within the expected range”

标签 c# windows-store-apps in-app-purchase desktop-application desktop-app-converter

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

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

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

我们按照下面提供的说明从使用桌面桥的 Windows 桌面应用程序的 UI 线程使用 C# 中的应用程序内购买。

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

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

[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() 返回一个错误,而不是显示用于购买产品的 Metro 界面。 这是返回的扩展错误:

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

HResult = -2147024809

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

最佳答案

在我们的 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(); }
}

关于c# - Windows Store In App Purchase问题 “Value does not fall within the expected range”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43762614/

相关文章:

ios - 使用 PDF 内容创建存档/.pkg 以在 Apple 托管时出现可执行文件的奇怪错误

C# - 整数的正确验证

c# - 在 dataGridView 中移动索引

c# - 适用于 Windows 应用商店应用程序的等宽字体

javascript - 如何访问Windows应用商店应用程序包中的文件

Flutter(IOS)内购本地收据

ios - SKProduct localizedTitle 总是英文

c# - 使用反射实例化类而不运行其构造函数

c# - Linq 到 SQL : Error deleting many to many relationship

c# - LaunchUriAsync() 不会启动 Windows 应用商店应用程序