4

I have a content page with toolbar added as follows

ContentPage

[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class ChartList : ContentPage
{
    public ChartList ()
    {
        InitializeComponent ();

        //public class ToolbarItem : MenuItem
        ToolbarItem settings = new ToolbarItem
        {
            Icon = "icon.png",
            Text = "Settings",
            Command = new Command(this.ShowHomePage),
        };

        this.ToolbarItems.Add(settings);
    }

    private void ShowHomePage()
    {
        this.Navigation.PushAsync(new MainPage());
    }
}

App.Xaml.cs

    public App()
    {
        InitializeComponent();

        ContentPage p = new MyHomeScreen2.MainPage();
        MainPage = new NavigationPage(p)
        {
            BarBackgroundColor = Color.Purple,
            BarTextColor = Color.White
        };
    }

I need to align the icon on the center of the toolbar. How to do it in Xamarin Forms?

enter image description here

Cfun
  • 8,442
  • 4
  • 30
  • 62
LCJ
  • 22,196
  • 67
  • 260
  • 418
  • 1
    I think you will need a custom renderer for this, check this out: https://stackoverflow.com/questions/36717697/xamarin-forms-position-of-toolbar-items-for-android?rq=1 – Paul Karam Jul 21 '17 at 21:55
  • @LIjo have you solved this issue? – R15 Apr 14 '18 at 11:40
  • @PaulKaram it is not required anymore if using Shell. – Cfun Dec 01 '20 at 14:40
  • @Cfun I haven't been into Xamarin for a long time now, so you are most likely right. I am gonna hope back in soon, so that's nice information to have. Thank you. – Paul Karam Dec 01 '20 at 14:59
  • @PaulKaram nice to have you back and have a bigger community using this amazing technology. Good to know it was helpful info. – Cfun Dec 01 '20 at 15:09

1 Answers1

1

I know at the time of the question Shell was probably not introduced yet, but now if your app is based on Shell you can achieve it easily without even a custom renderer, and also customize your toolbar using Shell.TitleView as Microsoft example:

<ContentPage ...>
    <Shell.TitleView>
        <Image Source="xamarin_logo.png"
               HorizontalOptions="Center"
               VerticalOptions="CenterAndExpand" />
    </Shell.TitleView>
    ...
</ContentPage>

It will probably not be perfectly centered because of a bug in Xamarin.forms Title View's content horizontal aligment

Cfun
  • 8,442
  • 4
  • 30
  • 62