xamarin / Xamarin.Forms

Xamarin.Forms is no longer supported. Migrate your apps to .NET MAUI.
https://aka.ms/xamarin-upgrade
Other
5.64k stars 1.88k forks source link

[Bug] CarouselView crashes on iOS when IsVisible="False" #15191

Open softlion opened 2 years ago

softlion commented 2 years ago

Description

When IsVisible is set to false on a CarouselView or one of its parents, it crashes on iOS simulator, even with an empty content template.

Xamarin.Forms.Platform.iOS\CollectionView\ItemsViewLayout.cs:415

Steps to Reproduce

<CarouselView IsVisible="False" ItemsSource="{Binding CarouselItems}" IndicatorView="{x:Reference TheIndicatorView}" HeightRequest="130" Loop="False" IsBounceEnabled="True" PeekAreaInsets="50">
    <CarouselView.ItemTemplate>
        <DataTemplate>
             <Label Text="toto" />
        </DataTemplate>
    </CarouselView.ItemTemplate>
</CarouselView>

Expected Behavior

Actual Behavior

Basic Information

Crash Logs

Foundation.MonoTouchException: Objective-C exception thrown.  Name: NSInternalInconsistencyException Reason: negative sizes are not supported in the flow layout
Native stack trace:
    0   CoreFoundation                      0x00007fff203feba4 __exceptionPreprocess + 242
    1   libobjc.A.dylib                     0x00007fff201a1be7 objc_exception_throw + 48
    2   Foundation                          0x00007fff20753aa2 _userInfoForFileAndLine + 0
    3   UIKitCore                           0x00007fff246fab4f -[UICollectionViewFlowLayout _getSizingInfosWithExistingSizingDictionary:] + 2915
    4   UIKitCore                           0x00007fff246fc0cc -[UICollectionViewFlowLayout _fetchItemsInfoForRect:] + 228
    5   UIKitCore                           0x00007fff246f4f89 -[UICollectionViewFlowLayout prepareLayout] + 220
    6   MyApp.iOS                        0x000000010e94815c xamarin_dyn_objc_msgSendSuper + 220
    7   ???                                 0x00000001174f39c2 0x0 + 4686035394
    8   MyApp.iOS                        0x000000010e701695 mono_jit_runtime_invoke + 1621
    9   MyApp.iOS                        0x000000010e827008 mono_runtime_invoke_checked + 136
    10  MyApp.iOS                        0x000000010e82a92e mono_runtime_invoke + 142
    11  MyApp.iOS                        0x000000010e93eb0c xamarin_invoke_trampoline + 6220
    12  MyApp.iOS                        0x000000010e946cd9 xamarin_arch_trampoline + 105
    13  MyApp.iOS                        0x000000010e947eb6 xamarin_x86_64_common_trampoline + 118
    14  UIKitCore                           0x00007fff246eea7d -[UICollectionViewData _prepareToLoadData] + 171
    15  UIKitCore                           0x00007fff246ef716 -[UICollectionViewData validateLayoutInRect:] + 51
    16  UIKitCore                           0x00007fff246b108f -[UICollectionView layoutSubviews] + 217
    17  UIKitCore                           0x00007fff2561d2b1 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 2962
    18  QuartzCore                          0x00007fff289e2d2e _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 548
    19  QuartzCore                          0x00007fff289edebf _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 65
    20  QuartzCore                          0x00007fff28929c86 _ZN2CA7Context18commit_transactionEPNS_11TransactionEdPd + 700
    21  QuartzCore                          0x00007fff28961853 _ZN2CA11Transaction6commitEv + 699
...

  at ObjCRuntime.Runtime.ThrowNSException (System.IntPtr ns_exception) [0x00000] in /Users/builder/azdo/_work/1/s/xamarin-macios/src/ObjCRuntime/Runtime.cs:449 
  at ObjCRuntime.Runtime.throw_ns_exception (System.IntPtr exc) [0x00000] in /Users/builder/azdo/_work/1/s/xamarin-macios/runtime/Delegates.generated.cs:392 
  at (wrapper native-to-managed) ObjCRuntime.Runtime.throw_ns_exception(intptr)
  at (wrapper managed-to-native) ObjCRuntime.Messaging.void_objc_msgSendSuper(intptr,intptr)
  at UIKit.UICollectionViewLayout.PrepareLayout () [0x00023] in /Users/builder/azdo/_work/1/s/xamarin-macios/src/build/ios/native/UIKit/UICollectionViewLayout.g.cs:496 
  at Xamarin.Forms.Platform.iOS.ItemsViewLayout.PrepareLayout () [0x00001] in D:\a\1\s\Xamarin.Forms.Platform.iOS\CollectionView\ItemsViewLayout.cs:415 

  at (wrapper managed-to-native) ObjCRuntime.Messaging.void_objc_msgSendSuper(intptr,intptr)
  at UIKit.UICollectionViewLayout.PrepareLayout () [0x00023] in /Users/builder/azdo/_work/1/s/xamarin-macios/src/build/ios/native/UIKit/UICollectionViewLayout.g.cs:496 
  at Xamarin.Forms.Platform.iOS.ItemsViewLayout.PrepareLayout () [0x00001] in D:\a\1\s\Xamarin.Forms.Platform.iOS\CollectionView\ItemsViewLayout.cs:415 
  at (wrapper managed-to-native) UIKit.UIApplication.UIApplicationMain(int,string[],intptr,intptr)
  at UIKit.UIApplication.Main (System.String[] args, System.Type principalClass, System.Type delegateClass) [0x0003b] in /Users/builder/azdo/_work/1/s/xamarin-macios/src/UIKit/UIApplication.cs:83 
    1   libobjc.A.dylib                     0x00007fff201a1be7 objc_exception_throw + 48
    2   Foundation                          0x00007fff20753aa2 _userInfoForFileAndLine + 0
    3   UIKitCore                           0x00007fff246fab4f -[UICollectionViewFlowLayout _getSizingInfosWithExistingSizingDictionary:] + 2915
    4   UIKitCore                           0x00007fff246fc0cc -[UICollectionViewFlowLayout _fetchItemsInfoForRect:] + 228
    5   UIKitCore                           0x00007fff246f4f89 -[UICollectionViewFlowLayout prepareLayout] + 220
    6   MyApp.iOS                        0x000000010e94815c xamarin_dyn_objc_msgSendSuper + 220
    7   ???                                 0x00000001174f39c2 0x0 + 4686035394
    8   MyApp.iOS                        0x000000010e701695 mono_jit_runtime_invoke + 1621
    9   MyApp.iOS                        0x000000010e827008 mono_runtime_invoke_checked + 136
    10  MyApp.iOS                        0x000000010e82a92e mono_runtime_invoke + 142
    11  MyApp.iOS                        0x000000010e93eb0c xamarin_invoke_trampoline + 6220
    12  MyApp.iOS                        0x000000010e946cd9 xamarin_arch_trampoline + 105
    13  MyApp.iOS                        0x000000010e947eb6 xamarin_x86_64_common_trampoline + 118
    14  UIKitCore                           0x00007fff246eea7d -[UICollectionViewData _prepareToLoadData] + 171
    15  UIKitCore                           0x00007fff246ef716 -[UICollectionViewData validateLayoutInRect:] + 51
    16  UIKitCore                           0x00007fff246b108f -[UICollectionView layoutSubviews] + 217
    17  UIKitCore                           0x00007fff2561d2b1 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 2962
    18  QuartzCore                          0x00007fff289e2d2e _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 548
    19  QuartzCore                          0x00007fff289edebf _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 65
    20  QuartzCore                          0x00007fff28929c86 _ZN2CA7Context18commit_transactionEPNS_11TransactionEdPd + 700
    21  QuartzCore                          0x00007fff28961853 _ZN2CA11Transaction6commitEv + 699
    22  QuartzCore                          0x00007fff28962bd4 _ZN2CA11Transaction25flush_as_runloop_observerEb + 60
    23  CoreFoundation                      0x00007fff2036bc67 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
    24  CoreFoundation                      0x00007fff2036648c __CFRunLoopDoObservers + 541
    25  CoreFoundation                      0x00007fff20366a3c __CFRunLoopRun + 1126
    26  CoreFoundation                      0x00007fff203660f3 CFRunLoopRunSpecific + 567
    27  GraphicsServices                    0x00007fff2c995cd3 GSEventRunModal + 139
    28  UIKitCore                           0x00007fff25059f42 -[UIApplication _run] + 928
    29  UIKitCore                           0x00007fff2505eb5e UIApplicationMain + 101
    30  ???                                 0x0000000116781245 0x0 + 4671935045
    31  ???                                 0x00000001167765cb 0x0 + 4671890891

Workaround

Remove PeekAreaInsets

bbenetskyy commented 2 years ago

@softlion thanks a lot your workaround helps for me as a temporary solution

grounch commented 1 year ago

App still crash when PeakAreaInsets is defined (Xamarin_Forms_Platform_iOS_CarouselViewLayout _getSizingInfosWithExistingSizingDictionary). Any chance to have a real fix soon other then just disabling the feature?

fabiansanchez18 commented 1 year ago

another workaround is to set PeekAreaInsets to 0 on XAML or code initialization and then change it with a DataTrigger linked to the one that has the IsVisible=false. This fixes the problem completely Example: ` <DataTrigger Binding="{Binding Source={Reference FatherFrame}, Path=IsVisible}" TargetType="CarouselView" Value="True">

   </DataTrigger>

</CarouselView.Triggers>`