From 7f93346a3d1d3261d05843084d1934d5201b0c75 Mon Sep 17 00:00:00 2001 From: Shane Neuville Date: Tue, 15 Oct 2019 12:44:22 -0600 Subject: [PATCH] Fix shell to correctly pick visible item (#8009) - when items are removed --- .../Issue8008.cs | 67 ++++++++++++++++++++++ .../Xamarin.Forms.Controls.Issues.Shared.projitems | 1 + Xamarin.Forms.Core/Shell/Shell.cs | 16 +++++- 3 files changed, 81 insertions(+), 3 deletions(-) create mode 100644 Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue8008.cs diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue8008.cs b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue8008.cs new file mode 100644 index 0000000..6b5d13b --- /dev/null +++ b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue8008.cs @@ -0,0 +1,67 @@ +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Text; +using Xamarin.Forms.CustomAttributes; +using Xamarin.Forms.Internals; + + +#if UITEST +using Xamarin.UITest; +using NUnit.Framework; +using Xamarin.Forms.Core.UITests; +#endif + +namespace Xamarin.Forms.Controls.Issues +{ + [Preserve(AllMembers = true)] + [Issue(IssueTracker.Github, 8008, "Removing Shell Item can cause Shell to try and set a MenuItem as the default visible item")] +#if UITEST + [NUnit.Framework.Category(UITestCategories.Shell)] +#endif + public class Issue8008 : TestShell + { + ShellItem item1; + protected override void Init() + { + item1 = AddContentPage(); + + Items.Add(new MenuShellItem(new MenuItem() + { + Text = "Menu Item", + Command = new Command(() => + { + throw new Exception("I shouldn't execute after removing an item"); + }) + })); + + AddContentPage(new ContentPage() + { + Content = new StackLayout() + { + Children = + { + new Label() + { + Text = "If you are reading this then this test has passed", + AutomationId = "Success" + } + } + } + }); + Device.BeginInvokeOnMainThread(() => + { + this.Items.Remove(item1); + }); + + } + +#if UITEST + [Test] + public void RemovingShellItemCorrectlyPicksNextValidShellItemAsVisibleShellItem() + { + RunningApp.WaitForElement("Success"); + } +#endif + } +} diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems index e51a2a8..3edebfa 100644 --- a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems +++ b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems @@ -13,6 +13,7 @@ + diff --git a/Xamarin.Forms.Core/Shell/Shell.cs b/Xamarin.Forms.Core/Shell/Shell.cs index 1779780..6183d53 100644 --- a/Xamarin.Forms.Core/Shell/Shell.cs +++ b/Xamarin.Forms.Core/Shell/Shell.cs @@ -783,7 +783,7 @@ namespace Xamarin.Forms { base.OnChildAdded(child); - if (child is ShellItem shellItem && CurrentItem == null && !(child is MenuShellItem)) + if (child is ShellItem shellItem && CurrentItem == null && ValidDefaultShellItem(child)) { ((IShellController)this).OnFlyoutItemSelected(shellItem); } @@ -793,12 +793,22 @@ namespace Xamarin.Forms { base.OnChildRemoved(child); - if (child == CurrentItem && Items.Count > 0) + if (child == CurrentItem) { - ((IShellController)this).OnFlyoutItemSelected(Items[0]); + for (var i = 0; i < Items.Count; i++) + { + var item = Items[i]; + if (ValidDefaultShellItem(item)) + { + ((IShellController)this).OnFlyoutItemSelected(item); + break; + } + } } } + bool ValidDefaultShellItem(Element child) => !(child is MenuShellItem); + internal override IEnumerable ChildrenNotDrawnByThisElement { get -- 2.7.4