Exception when linking to Microsoft Store Purchase API library from Blazor MAUI Windows App

Richard Freytag 61 Reputation points
2024-09-06T17:05:40.2066667+00:00

Hi, I am have written multile Microsoft Store Apps in the UWP and WPF frameworks and published them to the Store. These apps all have to and do call the Microsoft Store Purchase API.

Now I'm building a Blazor MAUI Store App and have found apps in the framework can be published to and downloaded from the Microsoft App Store.

However, I encounter a conflict when attempting to call the Microsoft Store Purchase API from my new Blazor MAUI app. This is a critical problem for me.

When I try to write two simple lines in my Blazor MAUI, Home.razor file as follows ... Trying to write two simple lines in Home.razor

#if WINDOWS
            StoreContext sc = StoreContext.GetDefault();
            var result = await sc.RequestPurchaseAsync("9N35H09Z21");
#endif

It throws error :

 System.Runtime.InteropServices.COMException
  HResult=0x80070578
  Message=Invalid window handle. (0x80070578)
  Consider WindowNative, InitializeWithWindow
  Source=WinRT.Runtime

I also tried this call using the way I had earlier had success with in WPF ...

IInitializeWithWindow initWindow = (IInitializeWithWindow)(object)sc; initWindow.Initialize(System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle);
An error occurred: System.InvalidCastException: Invalid cast from 'Windows.Services.Store.StoreContext' to 'MySecureNotes.Components.Pages.Home+IInitializeWithWindow'

Two questions:

  1. Do I need to add any special nuget packages ?
  2. Can anyone guide with a working example of how to call the Microsoft Store Purchase API from a Blazor MAUI apps that compiles?

Note: Although I'm developing a MAUI Blazor app, I'm currently limited to using it only on Windows. I am doing my development in VS Pro v17.11.2 with all libraries updated to their latest versions.

Thank you, very much,
Richard Freytag

Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,580 questions
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,514 questions
Microsoft Partner Center API
Microsoft Partner Center API
Microsoft Partner Center: A Microsoft website for partners that provides access to product support, a partner community, and other partner services.API: A software intermediary that allows two applications to interact with each other.
339 questions
{count} votes

1 answer

Sort by: Most helpful
  1. philip bray 0 Reputation points
    2024-10-04T15:21:14.22+00:00

    I managed to solve my issue which is exactly the same as the above issue by using the following code:

    Hope this helps!

    StoreContext context=StoreContext.GetDefault();
    var hwnd = ((MauiWinUIWindow)App.Current.Windows[0].Handler.PlatformView).WindowHandle;  
    WinRT.Interop.InitializeWithWindow.Initialize(context,hwnd);
    var storePurchaseResult=await context.RequestPurchaseAsync(storeId);
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.