在 UWP 应用程序中购买 IAP 后未处理的崩溃 - Windows 10

Unhandled crash after purchasing IAP in UWP app - Windows 10

我正在将应用内购买 IAP 添加到我的 UWP 应用中,但是在测试该过程时,应用崩溃了,但只有在通过调用时显示的测试对话框:

await CurrentAppSimulator.RequestProductPurchaseAsync(
      IAPs.CoreFeatures, false);

这是我的 ViewModel 中包含的完整函数:

private async void UnlockFeaturesNow()
{
    LicenseInformation licenseInformation = App.LicenseInformation;

    if (!licenseInformation.ProductLicense[IAPs.CoreFeatures].IsActive)
    {
        try
        {
            // The customer doesn't own this feature, so show the purchase dialog.
            await CurrentAppSimulator.RequestProductPurchaseAsyn(
                  IAPs.CoreFeatures, false);

            //Check the license state to determine if the in-app purchase was successful.
            if (!licenseInformation.ProductLicenses[IAPs.CoreFeatures].IsActive)
            {
              await this._messageBoxService.Show("features were not purchased.", 
                    "Features");
            }
            else
            {
                await this._messageBoxService.Show("features were purchased 
                successfully.", "Features");

                //Remove the purchase button from splitview popup menu.
                SectionModel unlockFeatureSection = this.Sections.FirstOrDefault
                (m => m.PageName == PageNameEnum.UnlockFeaturesPage);

                if (unlockFeatureSection != null)
                {
                    this.Sections.Remove(unlockFeatureSection);
                    unlockFeatureSection = null;
                }
            }
        }
        catch (Exception ex)
        {
            // The in-app purchase was not completed because an error occurred.
            await this._messageBoxService.Show("Failed to purchase the features for 
                  the following reason: " + ex.Message, "Features");
        }
    }
    else
    {
        // The customer already owns this feature.
    }
}

如果我取消 'IAP test dialog' 或者如果我 select 除了 OK 之外的任何其他选项,我的应用程序永远不会崩溃。

如果我 select 好,它告诉我我的 IAP 已成功购买,并且根据我在我的应用程序中的位置,它按预期工作,但第二次我点击我的汉堡菜单时,它显示我的SplitView 菜单,如果我在我的应用程序上单击其他任何地方,它就会崩溃并显示以下错误:

"A debugger is attached to myApp.exe but not configured to debug 
this unhandled exception. To debug this exception, detach the current
debugger."

我检查了我的事件查看器以获取更多信息,这是我发现的:

Faulting application name: MyApp.exe, version: 1.0.0.0, time stamp: 0x563304b4
Faulting module name: combase.dll, version: 10.0.10586.103, time stamp: 0x56a84cbb
Exception code: 0xc000027b
Fault offset: 0x00166d7e
Faulting process ID: 0x2138
Faulting application start time: 0x01d17a76e526db18
Faulting application path: C:\MyFolder\MyApp\bin\x86\Debug\AppX\MyApp.exe
Faulting module path: C:\WINDOWS\SYSTEM32\combase.dll
Report ID: bb041374-507f-4927-84b8-75bf7cb6df63
Faulting package full name: MyApp1.1.25.0_x86__z2199h13vtehs
Faulting package-relative application ID: App

所以看起来 Combase.dll 崩溃了,但我不知道这个 .dll 是干什么用的,为什么它只在 selecting 'OK' 而不是另一个之后崩溃选项。

我刚刚尝试了 Microsoft 提供的 UWP Store 示例,它使用 SplitView 并提供与我正在使用的相同的 IAP(场景 2)选项,当我' m selecting OK 在购买 'IAP Test' 对话框中,一切都按预期工作,所以它会让你认为这与我的代码有关,但我很确定它不是因为应用程序在使用时永远不会崩溃,并且在显示 IAP 测试对话框时永远不会崩溃,除非我 select 'OK' 选项。

明天我会继续调查,因为我的应用程序和 Microsoft 示例之间的一大区别是我使用的是 MVVM 并且此代码在我的 ViewModel 而不是 Code-Behind 来自 XAML 页。

如果您遇到过这个问题并且已经设法解决了它,您能告诉我吗?

谢谢。

我认为它崩溃了,因为您应该将此代码用于您的 licenseInformation

#if DEBUG
        LicenseInformation licenseInformation = CurrentAppSimulator.LicenseInformation;

#else
        LicenseInformation licenseInformation = CurrentApp.LicenseInformation;

我只是侥幸找到了我的问题的答案,但我仍然无法相信 .NET 处理得如此糟糕。

罪魁祸首是在成功购买 IAP 后从 SplitView 菜单中包含的 Listbox 中删除 "Purchase" 选项!与 CurrentAppCurrentAppSimulator 无关:

//Remove the purchase button from splitview popup menu.
SectionModel unlockFeatureSection = this.Sections.FirstOrDefault
(m => m.PageName == PageNameEnum.UnlockFeaturesPage);

if (unlockFeatureSection != null)
{
    this.Sections.Remove(unlockFeatureSection);
    unlockFeatureSection = null;
}

不刷新 SplitView 菜单中绑定到我的 Listbox 的项目列表(在本例中为部分)。所以现在,当删除该项目时,我确保通过 mvvmlight 引发一个事件,这将在我的 MainViewModel 中重建我的列表。

在我的 MainViewModel 中,构造函数中有以下内容:

Messenger.Default.Register<bool>(this, 
Tokens.LicenseInformationChanged, (value) =>
{
    InitializeSections();
});

现在我已经将我的版块从 List 更改为 ObservableCollection:

private void InitializeSections()
{
    this.Sections = (this.Sections 
       ?? new ObservableCollection<SectionModel>());

    this.Sections.Clear(); 

    .... Add sections (or not!)
}

但是,再一次,.NET 处理这个问题的方式多么可怕!轰炸我的应用程序抛出错误而没有抛出适当的异常真是太可怕了!

希望这对其他人有帮助。