在 Xamarin Forms 中居中对齐工具栏项目 [英] Center align toolbar items in Xamarin Forms

查看:22
本文介绍了在 Xamarin Forms 中居中对齐工具栏项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a content page with toolbar added as follows

内容页面

[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 表单中执行此操作?

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

推荐答案

我在提问时就知道 Shell 可能还没有引入,但现在如果你的应用基于 Shell 你可以实现它很容易无需甚至自定义渲染器,还可以使用 Shell.TitleView 作为微软示例:

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>

由于 Xamarin.forms 中的错误,它可能不会完全居中 标题视图内容水平对齐

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

这篇关于在 Xamarin Forms 中居中对齐工具栏项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆