当我按下手机上的取消或返回按钮时,我在检测Windows Phone 8的单元测试(Beta应用)期间应用内购买商店引发的异常时遇到问题。该应用程序只是退出。
使用MockIAP时没有错误。取消或后退按钮在await receipt = Store..
期间返回空收据变量。它在MockIAP中正确处理。但显然单元测试和真正的应用程序商店处理取消或返回事件的方式不同。该应用程序只是退出,我相信因为它抛出了未处理的错误。
我的应用是Phonegap 2.3,购买部分由插件处理。与MockIAP不同,在购买过程中按下“取消”或“返回”按钮时,我看不到(即附加断点)包装器侧发生的情况。我已尝试为购买的每一步显示MessageBox.Show
。当我按下确认购买时MessageBox.Show
代码正在工作,但是当我按下取消或后退按钮时没有。我已经将它与EventWaitHandle
同步了。
此外,我为未处理的Exception事件设置了e.Handled = true
,试图阻止它退出应用程序而没有运气。
在网上,我的购买代码是样板,所以我不明白为什么其他人之前没有遇到过这个问题,以及为什么没有在线解决方案。有没有人知道如何解决这个问题?
Purchase.cs(插件):
private static string receipt;
private async void purchaseProduct()
{
bool canBuy = false;
try
{
li = await Store.CurrentApp.LoadListingInformationAsync();
if (li.ProductListings.ContainsKey(package_id))
{
canBuy = true;
EventWaitHandle Wait = new AutoResetEvent(false);
Deployment.Current.Dispatcher.BeginInvoke(async () =>
{
// Here is the problem.. Don't know what is passed back to receipt when Cancel or Back is pressed, which is causing the app to close during Unit Test but not MockIAP
receipt = await Store.CurrentApp.RequestProductPurchaseAsync(package_id, true);
receipt = receipt.ToString();
Wait.Set();
});
Wait.WaitOne();
}
}
catch(Exception e)
{
var eMsg = e.Message.ToString();
errorMsg("Catch Exception: ", eMsg);
DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR));
}
finally
{
errorMsg("Receipt with await: ", receipt);
if (canBuy && receipt!= "")
{
errorMsg("Hitting the parsing", "");
parseXML(receipt);
prepData();
httpPostData();
Store.CurrentApp.ReportProductFulfillment(package_id);
}
else
{
errorMsg("Else Finally", "");
DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR));
}
}
}
private static void errorMsg(String caption, String msg)
{
EventWaitHandle Wait = new AutoResetEvent(false);
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
MessageBox.Show(caption + msg);
Wait.Set();
});
Wait.WaitOne();
}
App.cs
private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
{
Exception ex = (Exception)e.ExceptionObject;
EventWaitHandle Wait = new AutoResetEvent(false);
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
MessageBox.Show("Unhandled Exception: " + ex.Message);
Wait.Set();
});
Wait.WaitOne();
// Stop from exiting..
e.Handled = true;
if (System.Diagnostics.Debugger.IsAttached)
{
// An unhandled exception has occurred; break into the debugger
//System.Diagnostics.Debugger.Break();
}
}
答案 0 :(得分:3)
要修复此问题,请在RequestProductPurchaseAsync方法调用周围附上try / catch,即使您有整个方法的try / catch ...
try
{
receipt = await CurrentApp.RequestProductPurchaseAsync("MyItem", false);
}
catch (Exception){}
.... other code