diff --git a/src/Controls/src/Core/Handlers/Shell/Windows/ShellView.cs b/src/Controls/src/Core/Handlers/Shell/Windows/ShellView.cs index e8b01a1bc21e..a7f15467a9a7 100644 --- a/src/Controls/src/Core/Handlers/Shell/Windows/ShellView.cs +++ b/src/Controls/src/Core/Handlers/Shell/Windows/ShellView.cs @@ -154,17 +154,25 @@ IEnumerable IterateItems(List> groups) foreach (var item in group) { + bool foundExistingVM = false; + // Check to see if this element already has a VM counter part foreach (var navItem in FlyoutItems) { if (navItem is NavigationViewItemViewModel viewModel && viewModel.Data == item) + { + foundExistingVM = true; yield return viewModel; + } } - yield return new NavigationViewItemViewModel() + if (!foundExistingVM) { - Data = item - }; + yield return new NavigationViewItemViewModel() + { + Data = item + }; + } } } } diff --git a/src/Controls/tests/DeviceTests/Elements/Shell/ShellTests.Windows.cs b/src/Controls/tests/DeviceTests/Elements/Shell/ShellTests.Windows.cs index bc7081c60994..e0609923a960 100644 --- a/src/Controls/tests/DeviceTests/Elements/Shell/ShellTests.Windows.cs +++ b/src/Controls/tests/DeviceTests/Elements/Shell/ShellTests.Windows.cs @@ -1,6 +1,7 @@ using System; using System.Collections; using System.Collections.Generic; +using System.Collections.ObjectModel; using System.Linq; using System.Threading.Tasks; using Microsoft.Maui.Controls; @@ -240,6 +241,36 @@ await CreateHandlerAndAddToWindow(shell, (handler) => }); } + [Fact(DisplayName = "Shell Has Correct Item Count")] + public async Task ShellContentHasCorrectItemCount() + { + SetupBuilder(); + + var content1 = new ShellContent(); + content1.Title = "Hello"; + content1.Route = $"..."; + + var content2 = new ShellContent(); + content2.Title = "World"; + content2.Route = $"..."; + + var shell = await CreateShellAsync((shell) => + { + shell.Items.Add(content1); + shell.Items.Add(content2); + }); + + await CreateHandlerAndAddToWindow(shell, (handler) => + { + shell.FlyoutBehavior = FlyoutBehavior.Flyout; + handler.PlatformView.UpdateMenuItemSource(); + + var items = handler.PlatformView.MenuItemsSource as ObservableCollection; + Assert.True(items.Count == 2); + return Task.CompletedTask; + }); + } + [Fact(DisplayName = "Single Shell Section with Multiple Children")] public async Task SingleShellSectionWithMultipleChildren() {