我使用monogame作为游戏框架,我尝试在UWP-App中实现应用内购买功能,当我调用{{{{ 1}}。它声明:
设置后无法更改线程模式。 (HRESULT的例外情况: 0x80010106(RPC_E_CHANGED_MODE))at Windows.ApplicationModel.Store.CurrentAppSimulator.RequestProductPurchaseAsync(字符串 productId)at Crocs_World__Xbox_Edition_.App.d__7.MoveNext()
这就是我在代码中所做的事情:
RequestProductPurchaseAsync
每当我调用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
替换为Task
,那么它也无法正常工作。如果我从app.xaml.cs或game.cs调用它也没关系。
有没有人知道我做错了什么?
谢谢,
哈利
答案 0 :(得分:0)
您必须在UI线程中调用await CurrentAppSimulator.RequestProductPurchaseAsync(...)
,因为如果您在发布模式下使用await CurrentApp.RequestProductPurchaseAsync(...)
运行它,则会在屏幕上显示“购买项目”内容对话框,该对话框仅在UI中可用线程。
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
{
await CurrentAppSimulator.RequestProductPurchaseAsync("product2");
};