【问题标题】:Adding a button to the title bar Xamarin Forms将按钮添加到标题栏 Xamarin Forms
【发布时间】:2016-09-08 06:46:32
【问题描述】:

还没有找到完全正确的答案。我想在 Xamarin 表单导航页面顶部的导航/标题栏中添加一个按钮。请注意,我需要知道一种适用于 android 的方法,我对尝试在这里找到仅适用于 iOS 的解决方案不感兴趣。这个按钮需要是一个图像按钮,因为我想要一种访问汉堡菜单的方式。

【问题讨论】:

  • 能否请您添加一些关于您已经尝试过的内容以及您使用过的任何代码 sn-ps 的更多详细信息?
  • 我尝试过的sn-ps代码都没有任何效果,所以我真的没有什么可显示的。

标签: c# xamarin xamarin.forms xamarin.android imagebutton


【解决方案1】:

使用来自YourPage.xaml.csToolbarItem

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

在 Xaml 中:

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

更新

Xamarin.Forms 在 3.2.0 中引入了TitleView,可以自定义导航的标题。

<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>

【讨论】:

  • 它在 xaml 中的外观如何?
  • @tytyryty:
  • @Kowalski - 我觉得答案很棒。但是你能告诉我,"Search_Clicked" 事件是从哪里捕获的吗?
  • 在 YourPage.xaml.cs private void Search_Clicked(object sender, EventArgs args){}
  • 我们可以在工具栏中添加AbsoluteLayout而不是一个图标,这样我们就可以将多个东西作为一个图标来管理吗?喜欢,使用徽章添加到购物车图标。
猜你喜欢
  • 1970-01-01
  • 2018-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-04
相关资源
最近更新 更多