【问题标题】:RequestProductPurchaseAsync throws “Cannot change thread mode after it is set”RequestProductPurchaseAsync 抛出“设置后无法更改线程模式”
【发布时间】:2017-10-19 07:28:38
【问题描述】:

我正在使用 monogame 作为游戏框架,我正在尝试在 UWP 应用中实现应用内购买功能,当我调用 RequestProductPurchaseAsync 时会引发异常。它指出:

设置后无法更改线程模式。 (HRESULT 的例外情况: 0x80010106 (RPC_E_CHANGED_MODE)) 在 Windows.ApplicationModel.Store.CurrentAppSimulator.RequestProductPurchaseAsync(字符串 产品 ID)在 Crocs_World__Xbox_Edition_.App.d__7.MoveNext()

这就是我在代码中所做的:

public async Task LoadInAppPurchaseProxyFileAsync()   
{
     StorageFolder proxyDataFolder = await Package.Current.InstalledLocation.GetFolderAsync("data");
     StorageFile proxyFile = await proxyDataFolder.GetFileAsync("in-app-purchase.xml");
     licenseChangeHandler = new LicenseChangedEventHandler(InAppPurchaseRefreshScenario);
     CurrentAppSimulator.LicenseInformation.LicenseChanged += licenseChangeHandler;
     await CurrentAppSimulator.ReloadSimulatorAsync(proxyFile);

    // setup application upsell message
    try
    {
        ListingInformation listing = await CurrentAppSimulator.LoadListingInformationAsync();
        var product1 = listing.ProductListings["product1"];
        var product2 = listing.ProductListings["product2"];               
    }
    catch (Exception e)
    {
        Debug.WriteLine("LoadListingInformationAsync API call failed:" + e);               
    }
}

private async void InAppPurchaseRefreshScenario()
{           
    Debug.WriteLine("InAppPurchaseRefreshScenario");
}

public async Task BuyFeature()
{
    LicenseInformation licenseInformation = CurrentAppSimulator.LicenseInformation;
    if (!licenseInformation.ProductLicenses["product2"].IsActive)
    {
        Debug.WriteLine("Buying Product 2...");
        try
        {
            await CurrentAppSimulator.RequestProductPurchaseAsync("product2");
            if (licenseInformation.ProductLicenses["product2"].IsActive)
            {
                Debug.WriteLine("You bought Product 2.");
            }
            else
            {
                Debug.WriteLine("Product 2 was not purchased.");
            }
        }
        catch (Exception e)
        {
            Debug.WriteLine("Unable to buy Product 2." + e);
        }
    }
    else
    {
        Debug.WriteLine("You already own Product 2.");
    }
}

每当我调用BuyFeature 时,它都会抛出异常。除非我在LoadInAppPurchaseProxyFileAsync 中直接调用它。然后我猜它似乎在同一个线程中。 如果我在这两种方法中都用void 替换Task,它也不起作用。如果我从 app.xaml.cs 或 game.cs 调用它也没关系。 有人知道我做错了什么吗?

谢谢,

哈利

【问题讨论】:

    标签: c# uwp monogame


    【解决方案1】:

    您必须在 UI 线程中调用 await CurrentAppSimulator.RequestProductPurchaseAsync(...),因为如果您在发布模式下使用 await CurrentApp.RequestProductPurchaseAsync(...) 运行它,它会在屏幕上显示“购买项目”内容对话框,这只能在 UI 线程中进行。

    await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
    {
        await CurrentAppSimulator.RequestProductPurchaseAsync("product2");
    };
    

    【讨论】:

      猜你喜欢
      • 2020-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-30
      • 2018-10-26
      • 2019-07-27
      • 2014-10-16
      • 2020-11-25
      相关资源
      最近更新 更多