Xamarin.Forms bug? System.ObjectDisposedException: Cannot access a disposed object

Hi,

My Android Xamarin.Forms application uses a Navigation stack to display various views, I often have a list on a screen (A) that once a cell is clicked I push onto the stack a new screen (B), the user completes some form fields and then clicks a button to save. Once they click the ‘save‘ button a task is created on the thread pool to asynchronously perform some work (no async/await and no dependency on any UI) and the view is popped back to screen (A).

Once the task is completed, it fires a simple event using the PubSub library, on the UI thread, so screen (A) has the opportunity to refresh from the local database and it does.

The problem is that when I go to drag the updated list on screen (A), the app crashes with an ObjectDisposedExceptionintermittently (like 1 in 50 times)...

System.ObjectDisposedException: Cannot access a disposed object.
Object name: ‘Android.Views.GestureDetector‘.
  at Java.Interop.JniPeerMembers.AssertSelf (Java.Interop.IJavaPeerable self) [0x00030] in /Users/builder/data/lanes/3819/c1d1c79c/source/Java.Interop/src/Java.Interop/Java.Interop/JniPeerMembers.cs:153
  at Java.Interop.JniPeerMembers+JniInstanceMethods.InvokeVirtualBooleanMethod (System.String encodedMember, Java.Interop.IJavaPeerable self, Java.Interop.JniArgumentValue* parameters) [0x00002] in /Users/builder/data/lanes/3819/c1d1c79c/source/Java.Interop/src/Java.Interop/Java.Interop/JniPeerMembers.JniInstanceMethods_Invoke.cs:57
  at Android.Views.GestureDetector.OnTouchEvent (Android.Views.MotionEvent ev) [0x0002c] in /Users/builder/data/lanes/3819/c1d1c79c/source/monodroid/src/Mono.Android/platforms/android-23/src/generated/Android.Views.GestureDetector.cs:1546
  at Xamarin.Forms.Platform.Android.VisualElementRenderer`1[TElement].Android.Views.View.IOnTouchListener.OnTouch (Android.Views.View v, Android.Views.MotionEvent e) [0x0003f] in C:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms.Platform.Android\VisualElementRenderer.cs:101
  at Android.Views.View+IOnTouchListenerInvoker.n_OnTouch_Landroid_view_View_Landroid_view_MotionEvent_ (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_v, System.IntPtr native_e) [0x00019] in /Users/builder/data/lanes/3819/c1d1c79c/source/monodroid/src/Mono.Android/platforms/android-23/src/generated/Android.Views.View.cs:3558
  at at (wrapper dynamic-method) System.Object:4eaabbb9-8c1c-4ea1-b505-44c325bbb1ab (intptr,intptr,intptr,intptr)

I‘m not sure why it says that I cannot access a disposed object, I‘m interacting with a list with a gesture detector, it‘s on-screen, so it‘s not disposed.

Is this a bug in Xamarin.Forms?

On a similar note, if I modify an MVVP property that notifies listeners that a property has changed (I always check it‘s on the UI thread first), then if that‘s bound to a Xamarin.Forms list but it‘s not currently on-screen, presumably Xamarin.Forms will not barf ? After-All it‘s just off-screen and should still bind okay?

I‘ve just tweaked one of my projects to explicitly set the CachingStrategy on the ListView to RecycleElement and it‘s not been seen since. However I believe it can have knock-on effects with Converters, but so far in my case it seems to be okay. It‘s too early to say conclusively whether it‘s fixed the issue (GestureRecognizer related ObjectDisposed), but I thought I‘d offer the update just in case it helps anyone else out.

Note the auto-complete in the XAML doesn‘t show up, so you‘ll have to add

<ListView x:Name="NoStartList" ItemsSource="{Binding PollingRecordInfos}" >
<ListViewCachingStrategy>RecycleElement</ListViewCachingStrategy>

</ListView>

This is actually the opposite of what @AdamP suggested...

If you have your ListViewCachingStrategy set to RecycleElement, try disabling that and see if that stops the issue.

I never explicitly set a CachingStrategy, the default is RetainElement and given that sometimes the stack trace shows RetainElement, then I thought swapping to RecycleElement might actually help.

Fingers crossed.

from:https://forums.xamarin.com/discussion/79315/xamarin-forms-bug-system-objectdisposedexception-cannot-access-a-disposed-object

时间: 2024-08-03 18:20:01

Xamarin.Forms bug? System.ObjectDisposedException: Cannot access a disposed object的相关文章

System.Net.Sockets.Socket SendAsync System.ObjectDisposedException: Cannot access a disposed object.

发生未处理的域异常! System.ObjectDisposedException: Cannot access a disposed object. Object name: 'System.Net.Sockets.Socket'. at System.Net.Sockets.Socket.SendAsync(SocketAsyncEventArgs e) 在进行Socket长连接时,若服务器或客户端出现异常时,另外一端对Socket对话进行操作时,程序会出现无法访问已释放的对象的问题.例如客

Cannot access a disposed object. A common cause of this error is disposing

异常信息: An unhandled exception was thrown by the application.System.ObjectDisposedException: Cannot access a disposed object. A common cause of this error is disposing a context that was resolved from dependency injection and then later trying to use t

Cannot access a disposed object in ASP.NET Core

Cannot access a disposed object in ASP.NET Core 楠木大叔???? 导航 常见原因 总结 ??对于.neter来说,在使用ASP.NET Core的过程中,大家或多或少会遇到这样的报错--"Cannot access a disposed object".出现这样的异常的原因有很多,如果它在调试的时候出现是很容易解决的,但是有些时候它在本地运行良好,等到你部署到生产环境才会表现出来.针对这个异常,请跟随我一起来分析和探究其根本原因. ??在

asp.net Core HttpClient 出现Cannot access a disposed object. Object name: &#39;SocketsHttpHandler&#39; 的问题。

ASP.NET Core 部署在Centos 中 偶尔出现 One or more errors occurred. (Cannot access a disposed object.Object name: 'SocketsHttpHandler'.) 之前的写法如下: var httpclientHandler = new HttpClientHandler(); httpclientHandler.ServerCertificateCustomValidationCallback = (m

使用Xamarin Forms+XLabs 完成 Camara access

1. 安装nugetXamarin.FormsXLabs.Forms 2. MainActivity.cs  (Android) public class MainActivity : XFormsApplicationDroid { /// <summary> /// Called when [create]. /// </summary> /// <param name="bundle">The bundle.</param> pro

Xamarin.Forms学习之Platform-specific API和文件操作

这篇文章的分享原由是由于上篇关于Properties的保存不了,调用SavePropertiesAsync()方法也不行,所以我希望通过操作文件的方式保存我的需要的数据,然后我看了一下电子书中的第二十章和需要相关知识的第九章,这篇文章中的内容则是我学习这两章的一点记录和分享,还是那样,有错请留言指正,谢谢! 不同的平台存在着一些特定的API,通过在电子书中两章的学习,实践一下如何调用这些API和将这些API封装成公共的库,供以后的项目调用.以一个显示平台信息的小实例开始做一个简单的演示,其运行效

Xamarin.Forms WebView

目前本地或网络的网页内容和文件加载 WebView是在您的应用程序显示Web和HTML内容的视图.不像OpenUri,这需要用户在Web浏览器的设备上,WebView中显示您的应用程序内的HTML内容. 本指南由以下几部分组成: 内容?- web视图支持各种内容源,包括嵌入的HTML,网页和HTML字符串. 导航?-网页视图包括导航到特定页面,回到主页. 活动?–监听并通过在WebView中用户所采取的行动作出回应. 性能?-了解的WebView的每个平台上的性能特点. 权限?-了解如何设置权限

Forms Android System Download

1 不用自己写下载文件的功能,直接调用系统下载服务 2 forms 接口 3 4 using System; 5 using System.Collections; 6 using System.Collections.Generic; 7 8 namespace 9 { 10 public interface ISystemDownloadFile 11 { 12 //下载文件 13 void DownloadThread (string strUrl,string str1); 14 } 1

DICOM:fo-dicom之C-STORE再分析‘解决System.ObjectDisposedException异常’

背景: 在我的[DICOM系列专栏]中希望尽量涵盖关于DICOM协议的所有知识,但是在具体到某个知识点的讲解时往往会穿插使用DICOM协议的多种开源实现,例如基于C++的dcmtk.基于C#的fo-dicom.甚至是最近更加入的基于Java的dcm4che.之所以穿插介绍多种开源实现,简单概括其用意有三,第一,开源实现各有利弊,为了方便大家自己动手练习,第一时间会选择对该知识点实现最简单的开源库:第二,开源库的实现过程中应用到了许多编码和工程的设计理念,直接查看源码有时候会看起来与DICOM协议