簡體   English   中英

如何從 UI 線程調用方法?

[英]How do you call a method from a UI thread?

我正在嘗試從多線程程序訪問 Microsoft Store。 該程序本身運行良好 - 沒有問題,但方法

Windows.Foundation.IAsyncOperation<StorePurchaseResult> purchase = product.RequestPurchaseAsync();

拋出異常:

“window 句柄無效。\r\n\r\n必須從 UI 線程調用此 function”

我的代碼如下...

        private void testButton_Click(object sender, EventArgs e)
        {
            //Dispatcher dispUI = Dispatcher.CurrentDispatcher;
            //dispUI.BeginInvoke((ThreadStart)delegate ()
            //{
            //    _ = SetStore();
            //});

            //Dispatcher.BeginInvoke(new Action(TestStuff));


            _ = SetStore();

        }

        [ComImport]
        [Guid("4AEEEC08-7C92-4456-A0D6-1B675C7AC005")]
        [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
        private interface IInitializeWithWindow
        {
            void Initialize(IntPtr hwnd);
        }


        private async Task SetStore()
        {
            try
            {
                StoreContext theStore = StoreContext.GetDefault();

                // "Unable to cast object of type 'Windows.Services.Store.StoreContext' to type 'IInitializeWithWindow'."
                // IInitializeWithWindow initWindow = (IInitializeWithWindow)(object)theStore;
                // initWindow.Initialize(System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle);

                StoreProductResult app = await theStore.GetStoreProductForCurrentAppAsync();

                // This works...
                StoreProduct product = app.Product;
                string title = product.Title;
                string price = product.Price.FormattedPrice;

                // This works...
                StoreAppLicense license = await theStore.GetAppLicenseAsync();
                bool trial = license.IsTrial;
                bool full = license.IsActive;

                // "Invalid window handle.\r\n\r\nThis function must be called from a UI thread" 
                StorePurchaseResult result = await theStore.RequestPurchaseAsync("9NRFBVGVGW8K");


                // "Invalid window handle.\r\n\r\nThis function must be called from a UI thread" 
                // Windows.Foundation.IAsyncOperation<StorePurchaseResult> purchase = product.RequestPurchaseAsync();
            }
            catch (Exception e)
            {
                int a = 1;
            }

        }

我已將 VS2019 UWP 安裝程序項目與商店相關聯,我認為這就是為什么其他方法無需使用 IInitializeWithWindow 轉換商店 object 即可返回正確答案的原因(這也會引發錯誤,但繞過它似乎可以讓代碼工作) .

我想我必須以某種方式進入 UI 線程 - go 不知道如何。 來自各地的各種例子似乎對我不起作用。 誰能幫我?

編輯:這是一個 .Net 程序,帶有 UWP 包裝器,創建 MSIX package。

對於 UWP,您需要使用以下代碼調用 UI 線程:

await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => {
    //UI code here
});

嘗試以下,它適用於 .net 6 wpf 應用程序

WinRT.Interop.InitializeWithWindow.Initialize(storeContext, new WindowInteropHelper(window).Handle);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM