diff --git a/src/Controls/src/Core/Handlers/Shell/Windows/ShellView.cs b/src/Controls/src/Core/Handlers/Shell/Windows/ShellView.cs index a2d73d03b60f..86628e1326e4 100644 --- a/src/Controls/src/Core/Handlers/Shell/Windows/ShellView.cs +++ b/src/Controls/src/Core/Handlers/Shell/Windows/ShellView.cs @@ -155,17 +155,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..16bc832a8c62 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,35 @@ 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); + }); + } + [Fact(DisplayName = "Single Shell Section with Multiple Children")] public async Task SingleShellSectionWithMultipleChildren() {