首页 > 代码库 > Xamarin.Forms bug? System.ObjectDisposedException: Cannot access a disposed object

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

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