这是我的代码
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Accept.Add(new
MediaTypeWithQualityHeaderValue("application/json"));
var jsonstring = await client.GetStringAsync("https://asdasdadasd" +
url);
发生此错误:
2019-03-29 11:18:43.901 Eliant.App.iOS [3859:3548233] [AppCenterCrashes]错误:+ [MSWrapperLogger MSWrapperLog:tag:level:] / 7未处理的异常:UIKit.UIKitThreadAccessException:UIKit一致性错误:你是调用只能从UI线程调用的UIKit方法。在UIKit.UIApplication.EnsureUIThread()[0x00020]中
Library / Frameworks / Xamarin.iOS.framework / Versions / 12.6.0.16 / src / Xamarin.iOS / UIK
它/ UIApplication.cs:89 / Uibit.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位于/ Library/Frameworks/Xamarin.iOS.framework/Versions/12.6.0.16/src/Xamarin.iOS/UIKit/UIGestureRecognizer.cs中的UIKit.UIGestureRecognizer.OnDispose()[0x00016] :41 / Uibit.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在Foundation.NSObject.Finalize()[0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.6.0.16/src/Xamarin.iOS/Foundation/NSObject2.cs:143
有任何想法吗?
注释掉等待client.GetStringAsync()使应用程序正常工作。我已经尝试在主线程上使用Device invoke运行它(尽管我认为没有任何理由我应该这样做吗?)我已经尝试过.result而不是等待,我尝试了getasync( ).content.readstringasync并且始终存在相同的问题。
看起来你需要在UI线程上进行此调用(来自错误内容)。
您是否尝试确保此代码在UI线程上运行?有关如何执行此操作的详细信息,请参阅this link。
基本上你应该做的事情如下:
InvokeOnMainThread ( () => {
// code which needs to be run on UI thread
});