Xamarin.forms: [Bug] ShellItem.Items.Clear() crashes when the ShellItem has bottom tabs

Created on 15 Jul 2019  路  7Comments  路  Source: xamarin/Xamarin.Forms

Description

When using shell, if you want to programmatically clear and redraw the bottom tabs on a shell page, you can't use Items.Clear to get rid of all the existing ones because it crashes. Investigation suggests that this is because it's trying to set properties on a tab that is no longer there.

Steps to Reproduce

  1. Create a ShellItem in xaml that contains tabs.
  2. In code, call Items.Clear() on your ShellItem.
  3. A null exception will occur on line 83 of ShellBottomNavViewAppearanceTracker.SetBackgroundColor, presumably because menuView.GetChildAt(index) returns null, either because index is -1 or menuView has no children.

Expected Behavior

The tabs are cleared and you're free to create new ones

Actual Behavior

Exception and crash

Basic Information

  • Version with issue: 4.0.0.425677
  • Last known good version: Unknown
  • IDE: Visual Studio 2019
  • Platform Target Frameworks:

    • iOS: Untested

    • Android: 9.0

shell 1 hacktoberfest 馃嵒 high in-progress bug

Most helpful comment

Edit

Adding an exception is probably not the best solution for this
See comment here
https://github.com/xamarin/Xamarin.Forms/pull/8079#issuecomment-546028951

Edit over

Most likely the fix for this will be to just throw an exception that Clear isn't supported for Shell

If you clear all the items then Shell has no idea what to display. It's like setting the MainPage on the app to NULL

We have work being done for an IsVisible property which will be the way to change visibility of items. For now I would just add what you are wanting to add then remove what you are wanting to remove

All 7 comments

Do you have a sample where the problem happens?, could you attach it?. Thanks in advance!

https://github.com/SFSteve/Xamarin-Demos/tree/master/Xaminals

If you run that then go to the Random page from the flyout menu, you'll get the error.

If it helps, here is another test app to demonstrate the issue.

The crash happens at AppShell.xaml.cs line 29.

PickerTestApp.zip

Edit

Adding an exception is probably not the best solution for this
See comment here
https://github.com/xamarin/Xamarin.Forms/pull/8079#issuecomment-546028951

Edit over

Most likely the fix for this will be to just throw an exception that Clear isn't supported for Shell

If you clear all the items then Shell has no idea what to display. It's like setting the MainPage on the app to NULL

We have work being done for an IsVisible property which will be the way to change visibility of items. For now I would just add what you are wanting to add then remove what you are wanting to remove

An IsVisible flag would be ideal and solve the actual issue we're having, so it's good to know there's one on the way.

closed by #8215

Hi, This is still broken in IOS, any plans on when this will be fixed?

even when the Tab has multiple ShellItems when I try to Remove the first item "Items.RemoveAt(0);" it still crashes.

Was this page helpful?
0 / 5 - 0 ratings