I've been having trouble implementing IAP for Windows Phone 8 on Monogame. I've been following the API documentation: WP8 In-App Purchase API
As Monogame/XNA updates differently to Silverlight apps, I'm running IAP on the UI thread. I've tested with a beta app on the store, and can load the Store Front UI and display my product. When I attempt to purchase, it seems like it makes the purchase, tries to go back to the game but crashes. Further loads of the app don't display the store at all after this.
I've searched everywhere for a solution for this with no luck. I'd be really grateful if anyone could help me with this.
Here is my code sample:
public static void InvokeOnUIThread(Action action)
{
#if WINDOWS8 || OPENGL
action();
#else
var dispatcher = System.Windows.Deployment.Current.Dispatcher;
if (dispatcher.CheckAccess() /*IsUIThread*/)
action();
else
dispatcher.BeginInvoke(action);
#endif
}
I use it to invoke InvokeOnUIThread(Part1Purchase);
async void Part1Purchase()
{
ListingInformation li = await CurrentApp.LoadListingInformationAsync();
string pID = li.ProductListings["img.1"].ProductId;
try
{
string receipt = await Windows.ApplicationModel.Store.CurrentApp.RequestProductPurchaseAsync(pID, false);
Part2Fulfillment();
}
catch (Exception ex)
{}
}
public void Part2Fulfillment()
{
var productLicenses = Windows.ApplicationModel.Store.CurrentApp.LicenseInformation.ProductLicenses;
Part3DeliverGold(productLicenses["img.1"], 50);
}
void Part3DeliverGold(ProductLicense license, int goldCount)
{
if (license.IsConsumable && license.IsActive)
{
gold += goldCount;
CurrentApp.ReportProductFulfillment(license.ProductId);
}
}
I also tries this method to execute on the UI thread, but had the same results (crashing on purchase completion)
private void RestoreTitleDelayed(int delay)
{
ThreadPool.QueueUserWorkItem((object o) =>
{
Thread.Sleep(delay);
System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() =>
{
Part1Purchase();
});
});
}