Windows.Services.Store WPF 在 subscriptionStoreProduct.RequestPurchaseAsync 失败

huangapple go评论58阅读模式
英文:

Windows.Services.Store WPF fails at subscriptionStoreProduct.RequestPurchaseAsync

问题

目标是.NET 7.0。
目标操作系统:10.0.22621.0
最低支持的操作系统:10.0.17763

在添加订阅代码之前,应用已在 Windows 商店获得认证。点击保存时,会检查订阅状态。

我们遵循了 https://learn.microsoft.com/en-us/windows/uwp/monetize/enable-subscription-add-ons-for-your-app 中的步骤,转换为 VB。

MS 商店报告说代码可以获得可用的订阅并且在 subscriptionStoreProduct.RequestPurchaseAsync 之前工作。但这里失败,显示 "无效的窗口句柄 (0x80070578)。考虑使用 WindowNative,InitializeWithWindow"。我们添加了 InitializeWithWindow 的代码,现在它失败并显示 "指定的转换无效"。额外的代码由 ''' 注释括起来。

似乎没有用于排除故障的沙箱,所以我们必须继续重新提交应用程序进行认证,并等待它失败。

我们应该如何修复这个问题?下面是发布的代码:

    Private context As StoreContext
    Private subscriptionStoreId As String = "************"
    Private subscriptionStoreProduct As StoreProduct

    Public Async Function SetupSubscriptionInfoAsync() As Task
        ' ... (省略其余代码)'
    End Function

    Private Async Function CheckIfUserHasSubscriptionAsync() As Task(Of Boolean)
        ' ... (省略其余代码)'
    End Function

    Private Async Function GetSubscriptionProductAsync() As Task(Of StoreProduct)
        ' ... (省略其余代码)'
    End Function

    Private Async Function PromptUserToPurchaseAsync() As Task
        ' ... (省略其余代码)'
    End Function

    Private Async Sub btnSave_Click(sender As Object, e As RoutedEventArgs) Handles btnSave.Click
        ' ... (省略其余代码)'
    End Sub
英文:

Target is .NET 7.0.
Target OS: 10.0.22621.0
Min Supported OS: 10.0.17763

App was certified in the windows store before adding the subscription code. On Save, it checks subscription status.

We followed https://learn.microsoft.com/en-us/windows/uwp/monetize/enable-subscription-add-ons-for-your-app converted to VB.

MS Store reports that the code gets available subscription and works until subscriptionStoreProduct.RequestPurchaseAsync. Here it failed saying "Invalid window handle. (0x80070578) Consider WIndowNative, InitializeWithWindow". We added code for InitialzeWithWindow and now it fails saying "Specified cast is not valid". Extra code is enclosed by ''''' comments

There doesn't appear to be a sandbox for troubleshooting this so we have to keep resubmitting the app for certification and waiting for it to fail.

How do we fix this? Code posted below:

Private context As StoreContext
Private subscriptionStoreId As String = "************"
Private subscriptionStoreProduct As StoreProduct

Public Async Function SetupSubscriptionInfoAsync() As Task
    Try
        context = StoreContext.GetDefault()
        Dim userOwnsSubscription As Boolean = Await CheckIfUserHasSubscriptionAsync()
        If userOwnsSubscription Then
            AppGood = True
            Exit Function
        End If
        subscriptionStoreProduct = Await GetSubscriptionProductAsync()
        Dim sku As StoreSku = subscriptionStoreProduct.Skus(0)
        If sku.SubscriptionInfo.HasTrialPeriod Then
            MessageBox.Show("7 day trial available", "Trial")
        Else
            MessageBox.Show(sku.SubscriptionInfo.BillingPeriod & " (Subscription Period)" & vbCrLf & sku.SubscriptionInfo.BillingPeriodUnit & " (Subscription Period Unit)", "Subscription")
        End If
        Await PromptUserToPurchaseAsync()
    Catch ex As Exception
        MessageBox.Show("Error accessing Windows Store for subscription status.", "SetupSubscriptionInfoAsync Error")
    End Try
End Function

Private Async Function CheckIfUserHasSubscriptionAsync() As Task(Of Boolean)
    Try
        Dim appLicense As StoreAppLicense = Await context.GetAppLicenseAsync()
        For Each addOnLicense In appLicense.AddOnLicenses
            Dim license As StoreLicense = addOnLicense.Value
            If license.SkuStoreId.StartsWith(subscriptionStoreId) Then
                If license.IsActive Then
                    Return True
                    Exit Function
                End If
            End If
        Next
    Catch ex As Exception
        MessageBox.Show(ex.Message, "CheckIfUserHasSubscriptionAsync Error")
    End Try
    Return False
End Function

Private Async Function GetSubscriptionProductAsync() As Task(Of StoreProduct)
    Try
        Dim result As StoreProductQueryResult = Await context.GetAssociatedStoreProductsAsync(New String() {"Durable"})
        For Each item In result.Products
            Dim product As StoreProduct = item.Value
            If product.StoreId = subscriptionStoreId Then
                Return product
                Exit Function
            End If
        Next
    Catch ex As Exception
        MessageBox.Show(ex.Message, "GetSubscriptionProductAsync Error")
        Return Nothing
    End Try
    Return Nothing
End Function

Private Async Function PromptUserToPurchaseAsync() As Task
    Try
'''''these 3 lines were added after inital error
        Dim mywin = New Window
        Dim hwnd = New WindowInteropHelper(mywin).Handle
        InitializeWithWindow.Initialize(subscriptionStoreProduct, hwnd)
'''''

        Dim result As StorePurchaseResult

        result = Await subscriptionStoreProduct.RequestPurchaseAsync()

        Select Case result.Status
            Case StorePurchaseStatus.Succeeded
                MessageBox.Show("Subscription purchase successful.", "Success")
                AppGood = True
                Exit Function
            Case StorePurchaseStatus.NotPurchased
                MessageBox.Show("Subscription purchase did not complete or was cancelled.", "Fail")
                Exit Function
            Case StorePurchaseStatus.ServerError, StorePurchaseStatus.NetworkError
                MessageBox.Show("Network or server error, please try again later.", "Fail")
                Exit Function
            Case StorePurchaseStatus.AlreadyPurchased
                MessageBox.Show("Subscription already active, no action necessary.", "Success")
                AppGood = True
                Exit Function
        End Select
    Catch ex As Exception
        MessageBox.Show(ex.Message, "RequestPurchaseAsync Error")
    End Try
End Function

Private Async Sub btnSave_Click(sender As Object, e As RoutedEventArgs) Handles btnSave.Click
    Await SetupSubscriptionInfoAsync()
    If AppGood Then
        Dim MainWin As New MainWindow
        MainWin.Show()
        Me.Close()
    Else
        MessageBox.Show("No valid license.", "No license")
    End If
End Sub

答案1

得分: 0

你无法从 WindowInteropHelper.Handle 中获取有效的窗口句柄,直到窗口被初始化。如果存在已打开的窗口,则可以使用它来获取用于 IInitializeWithWindow.Initialize 方法的窗口句柄。

或者,你可以使用 WindowInteropHelper.EnsureHandle 方法来让窗口创建其窗口句柄并检索它。

Dim mywin = New Window
Dim hwnd = New WindowInteropHelper(mywin).EnsureHandle
英文:

You cannot get a valid window handle from WindowInteropHelper.Handle until the Window is initialized. Use existing opened Window, if any, to get a window handle for IInitializeWithWindow.Initialize method.

Or you can use WindowInteropHelper.EnsureHandle method to make the Window to create its window handle and retrieve it.

Dim mywin = New Window
Dim hwnd = New WindowInteropHelper(mywin).EnsureHandle

huangapple
  • 本文由 发表于 2023年6月16日 07:25:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76486079.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定