我在Monogame上为Windows Phone 8实施IAP时遇到了麻烦。我一直在关注API文档:WP8 In-App Purchase API
由于Monogame / XNA与Silverlight应用程序的更新方式不同,我在UI线程上运行IAP。我已在商店中测试了测试版应用,并可以加载Store Front UI并显示我的产品。当我试图购买时,它似乎是购买,试图回到游戏但崩溃。在此之后,应用程序的进一步加载根本不显示商店。
我到处寻找解决方案,但没有运气。如果有人能帮助我,我真的很感激。
这是我的代码示例:
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
}
我用它来调用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);
}
}
我也尝试在UI线程上执行此方法,但结果相同(在购买完成时崩溃)
private void RestoreTitleDelayed(int delay)
{
ThreadPool.QueueUserWorkItem((object o) =>
{
Thread.Sleep(delay);
System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() =>
{
Part1Purchase();
});
});
}
答案 0 :(得分:1)
我不确定你的情况究竟是什么问题。但我们有一个MonoGame项目已经在商店和应用内工作很好。我们在与Store进行交互时也崩溃了,但最终还是让它运转起来了。这是我们目前的做法:
在GamePage.xaml.cs中我们有:
...
using StoreExitAction = System.Action<string, string, bool>;
...
private static StoreExitAction StoreExitCallback;
....
public void LaunchStoreForProductPurchase(string productID, bool requestReceipt, StoreExitAction storeExitCallback)
{
StoreExitCallback = storeExitCallback;
Dispatcher.BeginInvoke(() => LaunchStoreForProductPurchaseASync(productID, requestReceipt));
}
private static async void LaunchStoreForProductPurchaseASync(string productID, bool requestReceipt)
{
bool productPurchaseError = false;
string receipt = "";
try {
receipt = await Store.CurrentApp.RequestProductPurchaseAsync(productID, requestReceipt);
}
catch (Exception ex)
{
productPurchaseError = true;
}
try
{
if (StoreExitCallback != null) StoreExitCallback(productID, receipt, productPurchaseError);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
要开始应用内购买,请执行以下操作:
GamePage.Instance.LaunchStoreForProductPurchase(productID, true, ReturnFromStoreCallback);