0

I am trying to implement in-app purchase into my WinRT appliation using desktop bridge and Windows App SDK with WinUI 3. I have simple method:

class TransactionService
{
   void buyOrRestore() {

       ....

       ComPtr<IAsyncOperation<StorePurchaseResult*>> purchaseOperation;
       hr = storeContext->RequestPurchaseWithPurchasePropertiesAsync(HStringReference(kItemStoreId).Get(), purchaseProperties.Get(), &purchaseOperation);
       CheckHr(hr);
   }
}

Following code always print error into output:

info:StoreContextServer::Initialize: packageFullName = PurchaseTester, productStoreId = 123, isLicensed = true, isAppContainer = false  [Windows::Services::Store::Internal::StoreContextServer::Initialize]
 info:Windows::Services::Store::StoreContext::RequestPurchaseWithPurchasePropertiesAsync(abc) invoked. (CV:8glMygpFo0+UMRKk.2.3)    [Windows::Services::Store::StoreContext::RequestPurchaseWithPurchasePropertiesAsync]
Exception thrown at 0x00007FFE7BB5474C (KernelBase.dll) in PurchaseTester.exe: WinRT originate error - 0x80070578 : 'This function must be called from a UI thread'.

buyOrRestore method is called directly from MainWindow.xaml.cpp after button click like:

void MainWindow::myButton_Click(IInspectable const&, RoutedEventArgs const&)
{
    auto transactionService = new TransactionService();
    transactionService->buyOrRestore();
}
IInspectable
  • 46,945
  • 8
  • 85
  • 181
JaSHin
  • 211
  • 2
  • 16
  • 43
  • So you are trying to create a windows app sdk application in c++/winrt, right? VS2019 contains a Winui3 application C++/WinRT project template, so you don't need to use the desktop bridge. Please download the template here: [Downloads for the Windows App SDK](https://learn.microsoft.com/en-us/windows/apps/windows-app-sdk/downloads). Then you could try it in the c++/winrt windows app SDK application again. – Roy Li - MSFT Aug 26 '22 at 08:39
  • This is created from template for Windows APP SDK with WinUI3. – JaSHin Aug 26 '22 at 11:30

1 Answers1

0

I don't know if this helps you but I had a similar issue of 'This function must be called from a UI thread'. I had my own (desktop) thread which needed to cause a UI event when a packet was received. The following code called from (desktop) thread gets the UI thread to instigate the call to raise the event from the UI thread.

  try
   {


      DispatcherQueue().TryEnqueue([this, hstringItem]()
                                   {
                                      eventPropertyChangedEventHandler(*this, PropertyChangedEventArgs(hstringItem));
                                   });
   }


   catch (...)
   {
   }
toyota Supra
  • 3,181
  • 4
  • 15
  • 19
Mart
  • 31
  • 2