Xamarin.Forms bug? System.ObjectDisposedException: Cannot access a disposed object
Posted 遥望星空
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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.
以上是关于Xamarin.Forms bug? System.ObjectDisposedException: Cannot access a disposed object的主要内容,如果未能解决你的问题,请参考以下文章
Xamarin.Forms 中的 Device.StartTimer 与 System.Threading.Timer [关闭]
有人可以帮我解决这个错误吗?(Xamarin.Forms)“System.Net.Http.HttpRequestException:'网络子系统已关闭'”
为啥我无法从我的 Xamarin.Forms UWP 应用程序访问 DownloadsFolder,并获得 System.UnauthorizedAccessException?
更改 ObservableCollection 时 Xamarin.Forms UWP (Windows) 应用程序中的 System.Runtime.InteropServices.COMExcep
升级到 Visual Studio 16.11.1 后,Xamarin Forms 破坏了 XANS 7028:System.IO.FileNotFoundException:无法加载程序集“Xama