Xamarin.Forms XAML - 向工具栏添加条目

时间:2017-06-28 16:16:24

标签: c# xaml xamarin xamarin.forms

在我的MasterDetail中,我有一个简单的页面:

<ContentPage xmlns="..."  Title="Detail">
  <ContentPage.ToolbarItems>
    <ToolbarItem Text="Hi" Icon="icon"><Entry></Entry></ToolbarItem>
  </ContentPage.ToolbarItems>

我正在尝试将一个条目或更好的自定义视图添加到工具栏中。如何访问工具栏以添加此类控件?

(基本上我需要在每个页面的工具栏上都有一个搜索文本框)

2 个答案:

答案 0 :(得分:1)

使用内置的ToolbarItems集合无法以跨平台方式执行此操作,但您可以使用自定义渲染器手动实现它。 This blogpost非常适用于Android和iOS解决方案的链接。对UWP来说同样容易实现。

答案 1 :(得分:0)

我使用此解决方案在页面顶部存档搜索收件箱。它不完全是工具栏项。我只是将工具栏隐藏在我想要的每个页面中。 它可能会更加灵活,因为它应该在每个平台上都能按预期工作。减号(如果有的话)需要在页面中实现工具栏项。 我将所有内容都绑定到viewmodel。

它可以在Xamarin 4.3上运行,但稍作修改就可以在早期版本上运行。

public SomeListPage()
{

    Shell.SetTabBarIsVisible(this, false);
    InitializeComponent();

}

这是我的整页。顶部包含文本输入和搜索按钮。然后列出项目。最后的奖励是用作新项目插入的圆形底部浮动按钮。对于cicle按钮,我使用带加号的fontawesome。

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
    x:Class="Puvx.Pages.SomeListPage"
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:d="http://xamarin.com/schemas/2014/forms/design"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"    
    mc:Ignorable="d">
    <StackLayout>
        <StackLayout Orientation="Horizontal">
            <Entry Text="Search box" />
            <Button Text="Seach" />
        </StackLayout>
        <AbsoluteLayout>
            <CollectionView ItemsSource="{Binding Reviews}">
                <CollectionView.ItemTemplate>
                    <DataTemplate>
                        <StackLayout Padding="10" Orientation="Vertical">
                            <Label FontAttributes="Bold" Text="{Binding Nr}" />                            
                        </StackLayout>
                    </DataTemplate>
                </CollectionView.ItemTemplate>
            </CollectionView>
            <Button
                AbsoluteLayout.LayoutBounds=".95,.95,80,80"
                AbsoluteLayout.LayoutFlags="PositionProportional"
                BackgroundColor="DarkGray"
                CornerRadius="50"
                FontFamily="{StaticResource FontAwesomeSolid}"
                FontSize="Large"
                Text="&#xf067;"
                TextColor="White" />
        </AbsoluteLayout>
    </StackLayout>
</ContentPage>