void MapMenuItems() { List <BaseShellItem> items; IShellItemController shellItemController = VirtualView; if (Routing.IsImplicit(VirtualView)) { items = new List <BaseShellItem>(((IShellSectionController)VirtualView.CurrentItem).GetItems()); } else { items = new List <BaseShellItem>(shellItemController.GetItems()); } bool hasTabs = shellItemController.ShowTabs; object?selectedItem = null; _mainLevelTabs.SyncItems(items, (navItem, baseShellItem) => { SetValues(baseShellItem, navItem); if (baseShellItem is not ShellSection shellSection) { navItem.MenuItemsSource = null; if (baseShellItem.Parent == VirtualView.CurrentItem) { selectedItem = navItem; } return; } var shellSectionItems = ((IShellSectionController)shellSection).GetItems(); if (shellSection == VirtualView.CurrentItem) { selectedItem = navItem; } if (shellSectionItems.Count <= 1) { if (navItem.MenuItemsSource != null) { navItem.MenuItemsSource = null; } } else { navItem.MenuItemsSource ??= new ObservableCollection <NavigationViewItemViewModel>(); navItem .MenuItemsSource .SyncItems(shellSectionItems, (shellContentNavItem, shellContent) => { SetValues(shellContent, shellContentNavItem); if (shellSection == VirtualView.CurrentItem && shellContent == VirtualView.CurrentItem.CurrentItem) { selectedItem = shellContentNavItem; } }); hasTabs = hasTabs || shellSectionItems.Count > 1; } void SetValues(BaseShellItem bsi, NavigationViewItemViewModel vm) { vm.Content = bsi.Title; var iconSource = bsi.Icon?.ToIconSource(MauiContext !); if (iconSource != null) { if (vm.Foreground != null) { iconSource.Foreground = vm.Foreground; } else if (PlatformView.Resources.TryGetValue("NavigationViewItemForeground", out object nviForeground) && nviForeground is WBrush brush) { iconSource.Foreground = brush; } } vm.Icon = iconSource?.CreateIconElement(); } });
void MapMenuItems() { // NavigationView makes a lot of changes to properties before it's been loaded // So we like to just wait until it's loaded to project our changes over it if (!ShellItemNavigationView.IsLoaded) { return; } IShellItemController shellItemController = VirtualView; var items = new List <BaseShellItem>(); // only add items if we should be showing the tabs if (shellItemController.ShowTabs) { foreach (var item in shellItemController.GetItems()) { if (Routing.IsImplicit(item)) { items.Add(item.CurrentItem); } else { items.Add(item); } } } object?selectedItem = null; _mainLevelTabs.SyncItems(items, (navItem, baseShellItem) => { SetValues(baseShellItem, navItem); if (baseShellItem is not ShellSection shellSection) { navItem.MenuItemsSource = null; if (baseShellItem.Parent == VirtualView.CurrentItem) { selectedItem = navItem; } return; } var shellSectionItems = ((IShellSectionController)shellSection).GetItems(); if (shellSection == VirtualView.CurrentItem) { selectedItem = navItem; } if (shellSectionItems.Count <= 1) { if (navItem.MenuItemsSource != null) { navItem.MenuItemsSource = null; } } else { navItem.MenuItemsSource ??= new ObservableCollection <NavigationViewItemViewModel>(); navItem .MenuItemsSource .SyncItems(shellSectionItems, (shellContentNavItem, shellContent) => { SetValues(shellContent, shellContentNavItem); if (shellSection == VirtualView.CurrentItem && shellContent == VirtualView.CurrentItem.CurrentItem) { selectedItem = shellContentNavItem; } }); } void SetValues(BaseShellItem bsi, NavigationViewItemViewModel vm) { vm.Content = bsi.Title; var iconSource = bsi.Icon?.ToIconSource(MauiContext !); if (iconSource != null) { if (vm.Foreground != null) { iconSource.Foreground = vm.Foreground; } else if (PlatformView.Resources.TryGetValue("NavigationViewItemForeground", out object nviForeground) && nviForeground is WBrush brush) { iconSource.Foreground = brush; } } vm.Icon = iconSource?.CreateIconElement(); } });