1

在 Windows Phone 8 的单元测试(Beta 应用程序)期间,当我按下手机上的取消或返回按钮时,我无法检测到应用内购买商店抛出的异常。该应用程序只是退出。

使用 MockIAP 时没有错误。取消或返回按钮在await receipt = Store... 它在 MockIAP 中得到了正确处理。但显然 Unit Test 和真正的应用商店以不同的方式处理 Cancel 或 Back 事件。该应用程序只是退出,我相信这是因为它抛出了一个未处理的错误。

我的应用是 Phonegap 2.3,购买部分由插件处理。与 MockIAP 不同,在购买过程中按下取消或后退按钮时,我看不到(即附加断点)包装端发生的情况。我尝试过展示MessageBox.Show购买的每一步。当MessageBox.Show我按下确认购买时,代码有效,但当我按下取消或返回按钮时无效。我已经让它与EventWaitHandle.

另外,我已经e.Handled = true为未处理的异常事件设置了尝试阻止它退出应用程序而没有运气。

从网上,我的购买代码是样板,所以我不明白为什么其他人以前没有遇到过这个问题,为什么网上没有解决方案。有谁知道如何解决这个问题?

购买.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();
        }

应用程序.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();
            }
        }
4

1 回答 1

3

修复这个在 RequestProductPurchaseAsync 方法调用周围附上 try/catch,即使您对整个方法都有一个 try/catch...

try
{
  receipt = await CurrentApp.RequestProductPurchaseAsync("MyItem", false);
}
catch (Exception){}

.... other code
于 2013-04-23T05:06:11.510 回答