Xamarin.Forms在与webView交互更新用户界面时 Java.Lang.RuntimeException: java.lang.Throwable: A WebView method was called on thread 'JavaBridge'. All WebView methods must be called on the same thread.

Xamarin.Forms安卓端与webview交互时, webview注册方法:WebViewMap.RegisterAction(data => ClickMapArea(data));在方法中更新用户界面时异常

原因:Xmarin.Froms访问用户界面元素的所有代码必须在应用程序的主线程上运行。

解决方式:利用Xamarin.Essentials:MainThread。MainThread 类允许应用程序在主执行线程上运行代码,并确定当前是否在主线程上运行特定代码块。

在需要更新用户界面的代码处添加:

MainThread.BeginInvokeOnMainThread(() =>
{
    //这里添加更新用户界面的代码
});

  

posted @ 2021-03-10 16:58  执念的代码之路  阅读(525)  评论(0)    收藏  举报