在 Xamarin Forms 中居中对齐工具栏项目

4

我有一个内容页面,工具栏添加如下:

内容页面

[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
        };
    }

我需要将工具栏上的图标居中对齐。在Xamarin Forms中如何实现?

enter image description here


1
我认为你需要一个自定义渲染器来解决这个问题,可以参考这个链接:https://dev59.com/HZbfa4cB1Zd3GeqPqjkw?rq=1 - Paul Karam
@LIjo,你解决了这个问题吗? - R15
@PaulKaram 如果使用Shell,则不再需要它。 - Cfun
@Cfun 我已经有很长一段时间没有接触Xamarin了,所以你很可能是正确的。我很快就会重新开始,这是一个不错的信息。谢谢。 - Paul Karam
@PaulKaram很高兴你回来了,并且有更多的社区使用这个令人惊叹的技术。很高兴知道这是有帮助的信息。 - Cfun
1个回答

1

我知道在提问时Shell可能还没有被介绍,但现在如果你的应用程序基于Shell,你可以很容易地实现它,甚至不需要自定义渲染器,并且使用Shell.TitleView自定义工具栏,就像微软的示例一样:

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

由于Xamarin.forms中的一个bug,标题视图的内容水平对齐可能不完全居中。Title View's content horizontal aligment

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接