【问题标题】:Center align toolbar items in Xamarin FormsXamarin Forms 中的中心对齐工具栏项
【发布时间】:2017-12-28 00:55:12
【问题描述】:

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

内容页面

[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 中进行操作?

【问题讨论】:

  • 我认为你需要一个自定义渲染器,看看这个:stackoverflow.com/questions/36717697/…
  • @LIjo 你解决了这个问题吗?
  • @PaulKaram 如果使用 Shell,则不再需要。
  • @Cfun 我已经很久没有进入 Xamarin 了,所以你很可能是对的。我希望很快回来,所以这是很好的信息。谢谢。
  • @PaulKaram 很高兴您能回来并拥有一个使用这项惊人技术的更大社区。很高兴知道这是有用的信息。

标签: c# xaml xamarin xamarin.forms


【解决方案1】:

我知道在提出问题时Shell 可能还没有介绍,但现在如果您的应用基于 Shell,您可以轻松实现它无需甚至自定义渲染器,还可以自定义使用 Shell.TitleView 作为 Microsoft 示例的工具栏:

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

由于 Xamarin.forms Title View's content horizontal aligment 中的错误,它可能不会完全居中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-18
    • 2020-02-18
    • 2020-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-18
    • 2019-02-10
    相关资源
    最近更新 更多