static void toggleTitleIcon(Page page) { var titleIcon = NavigationPage.GetTitleIconImageSource(page); if (titleIcon == null) { NavigationPage.SetTitleIconImageSource(page, "coffee.png"); } else { NavigationPage.SetTitleIconImageSource(page, null); } }
void UpdateTitleIcon() { Page currentPage = CurrentPage; if (currentPage == null) { return; } ImageSource source = NavigationPage.GetTitleIconImageSource(currentPage); if (source == null || source.IsEmpty) { Toolbar.RemoveView(_titleIconView); _titleIconView?.Dispose(); _titleIconView = null; _imageSource = null; return; } if (_titleIconView == null) { _titleIconView = new Android.Widget.ImageView(NavigationLayout.Context); Toolbar.AddView(_titleIconView, 0); } if (_imageSource != source) { _imageSource = source; _titleIconView.SetImageResource(global::Android.Resource.Color.Transparent); ImageSourceLoader.LoadImage(source, MauiContext, (result) => { _titleIconView.SetImageDrawable(result.Value); AutomationPropertiesProvider.AccessibilitySettingsChanged(_titleIconView, source); }); } }
public NavigationBarGallery(NavigationPage rootNavPage) { _rootNavPage = rootNavPage; int toggleBarTextColor = 0; int toggleBarBackgroundColor = 0; ToolbarItems.Add(new ToolbarItem { Text = "Save" }); NavigationPage.SetTitleIconImageSource(this, "coffee.png"); SearchBar searchBar = new SearchBar { HeightRequest = 44, WidthRequest = 100 }; // Note: Large and complex controls, such as ListView and TableView, are not recommended. var controls = new List <View> { searchBar, new ActivityIndicator { IsRunning = true }, new BoxView { BackgroundColor = Colors.Red }, new Button { Text = "Button!" }, new DatePicker { }, new Editor { Text = "Editor" }, new Entry { Placeholder = "Entry" }, new Image { Source = "crimson.jpg", HeightRequest = 44 }, new Label { Text = "Title View Label!" }, new Picker { ItemsSource = Enumerable.Range(0, 10).Select(i => $"Item {i}").ToList(), Title = "Picker" }, new ProgressBar { Progress = 50 }, new Slider { }, new Stepper { }, new Switch { }, new TimePicker { } }; int idx = 0; NavigationPage.SetTitleView(this, CreateTitleView(controls[idx])); rootNavPage.On <Android>().SetBarHeight(450); rootNavPage.On <iOS>().SetPrefersLargeTitles(false); Content = new ScrollView { Content = new StackLayout { Children = { new Button { Text = "Go to SearchBarTitlePage", Command = new Command(() => { rootNavPage.PushAsync(new SearchBarTitlePage(rootNavPage)); }) }, new Button { Text = "Change BarTextColor", Command = new Command(() => { if (toggleBarTextColor % 2 == 0) { rootNavPage.BarTextColor = Colors.Teal; } else { rootNavPage.BarTextColor = null; } toggleBarTextColor++; }) }, new Button { Text = "Change BarBackgroundColor", Command = new Command(() => { if (toggleBarBackgroundColor % 2 == 0) { rootNavPage.BarBackgroundColor = Colors.Navy; } else { rootNavPage.BarBackgroundColor = null; } toggleBarBackgroundColor++; }) }, new Button { Text = "Change Both to default", Command = new Command(() => { rootNavPage.BarTextColor = null; rootNavPage.BarBackgroundColor = null; }) }, new Button { Text = "Black background, white text", Command = new Command(() => { rootNavPage.BarTextColor = Colors.White; rootNavPage.BarBackgroundColor = Colors.Black; }) }, new Button { Text = "Toggle TitleIcon", Command = new Command(() => { var titleIcon = NavigationPage.GetTitleIconImageSource(this); if (titleIcon == null) { titleIcon = "coffee.png"; } else { titleIcon = null; } NavigationPage.SetTitleIconImageSource(this, titleIcon); }) }, new Button { Text = "Toggle TitleView", Command = new Command(() => { var titleView = NavigationPage.GetTitleView(this); if (titleView == null) { titleView = CreateTitleView(controls[idx]); } else { titleView = null; } NavigationPage.SetTitleView(this, titleView); }) }, new Button { Text = "Next TitleView", Command = new Command(() => { idx++; if (idx >= controls.Count) { idx = 0; } var titleView = CreateTitleView(controls[idx]); NavigationPage.SetTitleView(this, titleView); }) }, new Button { Text = "Toggle Back Title", Command = new Command(() => { var backTitle = NavigationPage.GetBackButtonTitle(rootNavPage); if (backTitle == null) { backTitle = "Go back home"; } else { backTitle = null; } NavigationPage.SetBackButtonTitle(rootNavPage, backTitle); }) }, new Button { Text = "Toggle Toolbar Item", Command = new Command(() => { if (ToolbarItems.Count > 0) { ToolbarItems.Clear(); } else { ToolbarItems.Add(new ToolbarItem { Text = "Save" }); } }) }, new Button { Text = "Toggle Title", Command = new Command(() => { if (Title == null) { Title = "NavigationBar Gallery - Legacy"; } else { Title = null; } }) }, new Button { Text = "Toggle BarHeight", Command = new Command(() => { if (rootNavPage.On <Android>().GetBarHeight() == -1) { rootNavPage.On <Android>().SetBarHeight(450); } else { rootNavPage.ClearValue(BarHeightProperty); } }) } } } }; }
protected virtual void UpdateToolbar() { ActionBarDrawerToggle toggle = _drawerToggle; if (Toolbar == null || NavigationStack.Count == 0 || CurrentPage == null || VirtualView == null) { return; } bool isNavigated = NavigationStack.Count > 1; Page currentPage = CurrentPage; _defaultNavigationIcon ??= Toolbar.NavigationIcon; if (isNavigated) { if (NavigationPage.GetHasBackButton(currentPage)) { Toolbar.NavigationIcon ??= _defaultNavigationIcon; if (toggle != null) { toggle.DrawerIndicatorEnabled = false; toggle.SyncState(); } var prevPage = (Page)NavigationStack[NavigationStack.Count - 2]; var backButtonTitle = NavigationPage.GetBackButtonTitle(prevPage); ImageSource image = NavigationPage.GetTitleIconImageSource(currentPage); if (!string.IsNullOrEmpty(backButtonTitle)) { Toolbar.NavigationContentDescription = backButtonTitle; } else if (image == null || Toolbar.SetNavigationContentDescription(image) == null) { Toolbar.SetNavigationContentDescription(Resource.String.nav_app_bar_navigate_up_description); } } else if (toggle != null && _flyoutPage != null) { toggle.DrawerIndicatorEnabled = _flyoutPage.ShouldShowToolbarButton(); toggle.SyncState(); } else { Toolbar.NavigationIcon = null; } } else { if (toggle != null && _flyoutPage != null) { toggle.DrawerIndicatorEnabled = _flyoutPage.ShouldShowToolbarButton(); toggle.SyncState(); Toolbar.SetNavigationContentDescription(Resource.String.nav_app_bar_open_drawer_description); } } Color tintColor = NavigationView.BarBackgroundColor; if (tintColor == null) { Toolbar.BackgroundTintMode = null; } else { Toolbar.BackgroundTintMode = PorterDuff.Mode.Src; Toolbar.BackgroundTintList = ColorStateList.ValueOf(tintColor.ToNative()); } Brush barBackground = NavigationView.BarBackground; Toolbar.UpdateBackground(barBackground); Color textColor = NavigationView.BarTextColor; if (textColor != null) { Toolbar.SetTitleTextColor(textColor.ToNative().ToArgb()); } Color navIconColor = NavigationPage.GetIconColor(CurrentPage); if (navIconColor != null && Toolbar.NavigationIcon != null) { DrawableExtensions.SetColorFilter(Toolbar.NavigationIcon, navIconColor, FilterMode.SrcAtop); } Toolbar.Title = currentPage?.Title ?? string.Empty; if (Toolbar.NavigationIcon != null && textColor != null) { var icon = this.Toolbar.NavigationIcon as DrawerArrowDrawable; if (icon != null) { icon.Color = textColor.ToNative().ToArgb(); } } UpdateTitleIcon(); UpdateTitleView(); UpdateToolbarVisibility(); }