桌面桥和 Windows APP SDK - 该函数必须从 UI 线程调用

问题描述 投票:0回答:1

我正在尝试使用桌面桥和带有 WinUI 3 的 Windows App SDK 在我的 WinRT 应用程序中实现应用内购买。我有简单的方法:

class TransactionService
{
   void buyOrRestore() {

       ....

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

以下代码始终将错误打印到输出中:

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

 调用 
MainWindow.xaml.cpp
方法,如下所示:

void MainWindow::myButton_Click(IInspectable const&, RoutedEventArgs const&)
{
    auto transactionService = new TransactionService();
    transactionService->buyOrRestore();
}
c++ windows desktop-bridge wrl windows-app-sdk
1个回答
0
投票

我不知道这是否对您有帮助,但我遇到了类似的问题“必须从 UI 线程调用此函数”。我有自己的(桌面)线程,需要在收到数据包时引发 UI 事件。从(桌面)线程调用的以下代码获取 UI 线程以发起调用以从 UI 线程引发事件。

尝试 {

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

}

抓住(...) { }

© www.soinside.com 2019 - 2024. All rights reserved.