在应用内购买时后退/取消按钮

本文关键字:取消 按钮 应用 | 更新日期: 2023-09-27 18:10:19

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

当我使用MockIAP时没有错误。取消或返回按钮在await receipt = Store..期间返回一个空收据变量。它在MockIAP中被正确处理。但显然,Unit Test和真正的app Store处理Cancel或Back事件的方式不同。应用程序只是退出,我认为这是因为它抛出了一个未处理的错误。

我的应用程序是一个Phonegap 2.3和购买部分是由插件处理。与MockIAP不同的是,当购买过程中按下取消或后退按钮时,我无法看到(即附加断点)包装器端发生了什么。我已经尝试在购买的每个步骤中显示MessageBox.ShowMessageBox.Show代码工作时,我按下确认购买,但不是当我按取消或后退按钮。我已经把它和EventWaitHandle同步了。

此外,我为未处理的异常事件设置了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();
            }
        }

在应用内购买时后退/取消按钮

修复这个封闭的try/catch围绕RequestProductPurchaseAsync方法调用,即使你有一个整个方法的try/catch…

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