gpt4 book ai didi

xamarin - UIKit.UIKitThreadAccessException : UIKit Consistency error: you are calling a UIKit method that can only be invoked from the UI thread

转载 作者:行者123 更新时间:2023-12-02 03:18:46 35 4
gpt4 key购买 nike

我正在尝试运行此代码:

GetRepsButton.Command = new Command(async () =>
{
var url = @"https://url";

HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

//crashes here
var test = await client.GetStringAsync(url);
});

当我这样做时,整个应用程序崩溃并且输出窗口显示此错误:

ROR: +[MSWrapperLogger MSWrapperLog:tag:level:]/7 Unhandled Exception:
UIKit.UIKitThreadAccessException: UIKit Consistency error: you are calling a UIKit method that can only be invoked from the UI thread.
at UIKit.UIApplication.EnsureUIThread () [0x00020] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.6.0.16/src/Xamarin.iOS/UIKit/UIApplication.cs:89
at UIKit.UIGestureRecognizer.RemoveTarget (Foundation.NSObject target, System.IntPtr action) [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.6.0.16/src/Xamarin.iOS/UIKit/UIGestureRecognizer.g.cs:342
at UIKit.UIGestureRecognizer.OnDispose () [0x00016] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.6.0.16/src/Xamarin.iOS/UIKit/UIGestureRecognizer.cs:41
at UIKit.UIGestureRecognizer.Dispose (System.Boolean disposing) [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.6.0.16/src/Xamarin.iOS/UIKit/UIGestureRecognizer.g.cs:959
at
Foundation.NSObject.Finalize () [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.6.0.16/src/Xamarin.iOS/Foundation/NSObject2.cs:143
Resolved pending breakpoint at 'SelectRep.xaml.cs:110,1' to void NameSpace.<ClientPicker_SelectedIndexChanged1>d__3.MoveNext () [0x0000c].

虽然我认为我不应该这样做,因为我没有对 UI 做任何事情,但我尝试过 InvokeOnMainThread ,但它不起作用。我还尝试更改为 .Result 而不是等待,并且尝试了 GetAsync 并读取内容,并使用 WebClient 而不是 HttpClient.所有这些都会导致同样的错误。

有什么想法吗?我正在使用 Xamarin forms 4 pre 7。

最佳答案

使用Device.BeginInvokeOnMainThread

Device.BeginInvokeOnMainThread(() =>
{
//your code
});

https://learn.microsoft.com/en-us/dotnet/api/xamarin.forms.device.begininvokeonmainthread?view=xamarin-forms

关于xamarin - UIKit.UIKitThreadAccessException : UIKit Consistency error: you are calling a UIKit method that can only be invoked from the UI thread,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55423904/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com