CollectionView throwing an exception if user back to the page using MasterDetailPage navigation (this is hard to explain, see steps to reproduce)
System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'SelectableItemsViewController'.
(Application.Current.MainPage as MasterDetailPage).Detail = new NavigationPage(new MainContentPage());
Working Detail page
Call stack:
0xC in System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw at /Library/Frameworks/Xamarin.iOS.framework/Versions/12.4.0.64/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:155,13 C#
0x6 in System.Runtime.CompilerServices.AsyncMethodBuilderCore.<>c.<ThrowAsync>b__6_0 at /Library/Frameworks/Xamarin.iOS.framework/Versions/12.4.0.64/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:1023,49 C#
0xC in Foundation.NSAsyncSynchronizationContextDispatcher.Apply at /Library/Frameworks/Xamarin.iOS.framework/Versions/12.4.0.64/src/Xamarin.iOS/Foundation/NSAction.cs:178,5 C#
0x5C in UIKit.UIApplication.UIApplicationMain C#
0xB in UIKit.UIApplication.Main at /Library/Frameworks/Xamarin.iOS.framework/Versions/12.4.0.64/src/Xamarin.iOS/UIKit/UIApplication.cs:79,4 C#
0x2F in UIKit.UIApplication.Main at /Library/Frameworks/Xamarin.iOS.framework/Versions/12.4.0.64/src/Xamarin.iOS/UIKit/UIApplication.cs:63,4 C#
> 0x8 in <MyApp>.iOS.Application.Main at <MyPath>\<MyApp>.iOS\Main.cs:12,13 C#
exact same code is working for Android, but not for iOS
Platform Target Frameworks:
Affected Devices: both simulator and physical device (5s)
@SUDALV92 Can you please attach a small project that demonstrates this issue? Thanks!
Is there a workaround to this bug while waiting for the release?
We get the same error but in a different case. We have a Tabbar and if I navigate through the list and set the SelectedItem to null before navigating with the tabbar the error mentioned here is occuring. Anything we can do for this?
@jsuvanto I found a workaround now...
Create a Renderer for CollectionView like this:
`public class CustomCollectionViewRenderer : CollectionViewRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs
{
try
{
if (e.NewElement == null)
return;
base.OnElementChanged(e);
}
catch(Exception)
{
// crash because e.NewElement is null after navigation
}
}
}`
The OnElementChanged is called after the navigation and the e.NewElement is null. Because of this the base method is crashing. So either make the if or try / catch (or just use both to be sure).
I'm having this same issue. Is the fix available yet?
Also running into this issue when removing a page containing a CollectionView from a TabbedPage's children.
Most helpful comment
@jsuvanto I found a workaround now... e)
Create a Renderer for CollectionView like this:
`public class CustomCollectionViewRenderer : CollectionViewRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs
{
try
{
if (e.NewElement == null)
return;
The OnElementChanged is called after the navigation and the e.NewElement is null. Because of this the base method is crashing. So either make the if or try / catch (or just use both to be sure).