Xamarin forms toolbar items not displaying on view

11,690

Solution 1

Your ToolbarItems will not show if your app does not have a Toolbar. The simplest way to add one is to wrap your page in a NavigationPage

public static Page GetMainPage ()
{   
    return new NavigationPage(new SamplePage ());
}

Solution 2

You only need to create a NavigationPage once per navigation stack. When you push TodoItemView, do this instead:

list.ItemTapped += (sender, args) => {
  Navigation.PushAsync (new TodoItemView ());
  // Reset the selected item
  list.SelectedItem = null;
};

TodoItemView will be pushed onto the navigation stack that is already contained within the first NavigationPage. Eliminating the extra NavigationPage will probably fix your missing toolbar item too. If it does not, also verify that the toolbar icon you specify is valid.

Solution 3

The simplest way to add one is to wrap your page in a NavigationPage object, and then call it like this:

var testPage = new NavigationPage(new TestPage());
Navigation.PushAsync(testPage);
Share:
11,690
noobie
Author by

noobie

Updated on June 04, 2022

Comments

  • noobie
    noobie almost 2 years

    I have a number of view pages that all inherit from ContentPage. The first page that is called from the app.cs displays the icon in the toolbar correctly. The second page (TodoItemView) does not display an icon at all (the same code is used). Is there something that I need to do to initialize the toolbar for the second view?

    thanks in advance.

    This page is called by the following code:
    
          var todoList = new TodoListView();
          return new NavigationPage(todoList);
    
    Th page is called by the following code:
    
          var todoList = new TodoListView();
          return new NavigationPage(todoList);
    
    
    
    
    // Works shows refresh icon
    public class TodoListView : ContentPage
        {
            public TodoListView ()
            {
                Title = "Social Events";
    
                // Display the refresh icon
                var toolbarItem = new ToolbarItem ("Refresh", "refresh.png", ()=>{
                }, 0, 0);
                ToolbarItems.Add (toolbarItem);
    
                var list = new ListView ();
                var viewModel = new TodoListViewModel ();
                list.ItemsSource = viewModel.TodoList;
    
                var cell = new DataTemplate (typeof(AspectImageCell));
                cell.SetBinding (TextCell.TextProperty, "Name");
                cell.SetBinding (TextCell.DetailProperty, "Details");
                cell.SetBinding (ImageCell.ImageSourceProperty, "Image");
                list.ItemTemplate = cell;
                list.ItemTapped += (sender, args) => {
    
    
                    Navigation.PushAsync (new NavigationPage(new TodoItemView ()));
    
                    // Reset the selected item
                    list.SelectedItem = null;
                };
    
                Content = list;
            }
        }
    
    // Does not work, does not show icon
    public class TodoItemView : ContentPage
        {
            public TodoItemView ()
            {
                Title = "Todo item";
    
                var toolbarItem = new ToolbarItem ("Send", "send.png", ()=>{
    
                    // email todo item
    
                }, 0, 0);
                ToolbarItems.Add (toolbarItem);
             }
    }