Adding a button to the title bar Xamarin Forms

Jobalisk picture Jobalisk · Sep 8, 2016 · Viewed 26.7k times · Source

Haven't been able to find quite the right answer for this yet. I want to add a button into the navigation / title bar at the top of a Xamarin Forms Navigation Page. Note that I need to know a method that will work on android, I'm not interested in trying to find an iOS only solution here. This button needs to be an image button as I want a way to access a hamburger menu.

Answer

eakgul picture eakgul · Sep 8, 2016

Use ToolbarItem from YourPage.xaml.cs:

ToolbarItems.Add(new ToolbarItem("Search", "search.png",() =>
{
  //logic code goes here
}));

In Xaml:

<ContentPage.ToolbarItems> 
    <ToolbarItem Icon="search.png" Text="Search" Clicked="Search_Clicked"/>
</ContentPage.ToolbarItems>

Update

Xamarin.Forms introduced TitleView in 3.2.0 that you can customize title of the navigation.

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="TestPage">
    <NavigationPage.TitleView>
        <Button ... />
    </NavigationPage.TitleView>
    ...
</ContentPage>