简体   繁体   中英

Xamarin.Forms XAML - Adding an Entry to a Toolbar

In my MasterDetail, I have a simple page:

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

I am trying to add an Entry, or better, a custom View into the Toolbar. How would I access the toolbar to add such a control?

(Bascially I need a search textbox on the toolbar on every page)

It is not possible to do this simply in a cross platform manner using the built in ToolbarItems collection, but you can implement it manually using custom renderers. [This blogpost] describes it very well, for both Android and also links to iOS solution. It will be similarly easy to implement for UWP.

I use this solution to archive search inbox on top of page. It is not exactly toolbar item. I just hide toolbar in every page i want. It could be even more flexible solution since it should work more as expected on every platform. Minus than you need to implement toolbar items in page if you had one. I bind everything to viewmodel.

It works on Xamarin 4.3, but with small modifications should work on earlier versions too.

public SomeListPage()
{

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

}

Here is my full page. Top contains text input and button for search. Then list of items. Bonus here last is circle bottom floating button to use as new item insert. For cicle button i use fontawesome with plus sign.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM