How to add a menu to a Xamarin.Forms ToolbarItem? - toolbar

I am looking for a way to add a menu to my ToolbarItem. Right now its just clickable text, here is an example of what I am trying to create,
Here is what I have so far:
<ContentPage.ToolbarItems>
<ToolbarItem Text="{Binding EditButtonText, Mode=TwoWay}" Clicked="EditClicked" />
</ContentPage.ToolbarItems>

You should be able to set the order of the ToolBarItem to Secondary to force the option into an overflow menu on Android:
<ContentPage.ToolbarItems>
<ToolbarItem Text="{Binding EditButtonText, Mode=TwoWay}" Clicked="EditClicked" Order="Secondary" />
</ContentPage.ToolbarItems>

Related

Size of the image in the toolbar

I wanted to ask if there was a way to enlarge the image size in the toolbar, because the aspect tag is not recognized
enter code here <ContentPage.ToolbarItems> <ToolbarItem IconImageSource="logo.JPG" x:Name="toolbar" /> </ContentPage.ToolbarItems>
We can not control the image size on the toolbaritem ,but as a workaround , NavigationPage.TitleView can be used to solve the problem .
Remove the ContentPage.ToolbarItems , set a ImageButton as TitleView , then adjust the image size .
<NavigationPage.TitleView>
<StackLayout>
<ImageButton Source="dog.png" WidthRequest="50" HeightRequest="50" HorizontalOptions="End"/>
</StackLayout>
</NavigationPage.TitleView>
Please check my test
Before (ToolbarItem)
After (TitleView)

Disable iOS autocomplete in Xamarin.Forms SearchBar

I have a Xamarin.Forms SearchBar to search for information on a remote server. This data is displayed in a ListView below the searchbar like the following:
Pseudocode:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage ... >
<ContentPage.Content>
<StackLayout>
<SearchBar
x:Name="SearchBar"
Placeholder="Search for a business..."
HorizontalTextAlignment="Start"
SearchCommand="{Binding PerformSearch}"
SearchCommandParameter="{Binding Text, Source={x:Reference SearchBar}}">
</SearchBar>
<ListView
x:Name="SearchResults"
ItemsSource="{Binding Path=SearchResults}"
...
>
</ListView>
</StackLayout>
</ContentPage.Content>
</ContentPage>
Is it possible to disable the autocomplete/autocorrect in the keyboard? I know for a Text.Entry it is possible to set IsTextPredictionEnabled to false but I don't see a way to configure a SearchBar in a similar way.
Yes, according to the SearchBar Properties, the Keyboard for the InputView is exposed. Therefore, similar to this answer, it can be achieved in the XAML by using the x:FactoryMethod attribute:
<SearchBar
x:Name="SearchBar"
Placeholder="Search for a business..."
HorizontalTextAlignment="Start"
SearchCommand="{Binding PerformSearch}"
SearchCommandParameter="{Binding Text, Source={x:Reference SearchBar}}">
<SearchBar.Keyboard>
<Keyboard x:FactoryMethod="Create">
<x:Arguments>
<KeyboardFlags>None</KeyboardFlags>
</x:Arguments>
</Keyboard>
</SearchBar.Keyboard>
</SearchBar>

Xamarin.Forms ToolbarItems Title

Is it possible to to disable title of current page in toolbarItems?
https://imgur.com/a/MsX0FKp
<ToolbarItem Text="{Binding positionNumber}"
/>
<ToolbarItem Text="PREV"
Clicked="previousQuestion"/>
<ToolbarItem Text="HINT"
/>
<ToolbarItem Text="NEXT"
Clicked="nextQuestion"/>
</ContentPage.ToolbarItems>
I've achieved it by simply adding
ToolbarItem Text=" 'space' PREV"
is there a proper way to do this?

Xamarin Forms Automation Id for toolbaritems

We are building Test Automation with Appium and we need to add Unique Identifiers for elements we are interacting with. We have achieved this through adding AutomationId property to the XAML file.
This works fine except for Toolbaritems. The added AutomationId is not visible as content-desc for the elements.
<core:AppBaseContentPage.ToolbarItems>
<ToolbarItem AutomationId="Button1" BindingContext="{Binding BindingContext, Source={x:Reference this}}" Parent="{x:Reference this}"
Text="{extensions:Translate Button1}"
Command="{Binding Button1Command}" />
Is this expected behaviour or am I missing something?
The AutomationId for ToolbarItems doesn't work (at least on Android), but you can identify them using the text property.
<NavigationPage.ToolbarItems>
<controls:BindableToolbarItem
AutomationId="ToolbarButtonHelp"
Text="ToolbarButtonHelp"
Icon="help"
Priority="0"
Order="Primary"
IsVisible="{Binding ToolbarService.IsHelpVisible, Mode=TwoWay}"
Command="{Binding ToolbarService.HelpCommand}"/>
<controls:BindableToolbarItem
AutomationId="ToolbarButtonMenu"
Text="ToolbarButtonMenu"
Icon="quick_menu"
Priority="1"
Order="Primary"
IsVisible="{Binding ToolbarService.IsMenuVisible, Mode=TwoWay}"
Command="{Binding ToolbarService.MenuCommand}"/>
</NavigationPage.ToolbarItems>
Example Xamarin UI Test
this.app.WaitForNoElement("ToolbarButtonHelp");

xamarin.forms add switch in toolbar

Is it possible to add a switch on the toolbar?
<ContentPage.ToolbarItems>
<ToolbarItem>
<Switch x:Name="Switch1" IsToggled="True" Margin="5,0,0,0"/>
</ToolbarItem>
</ContentPage.ToolbarItems>
Does anybody have any ideas?
Thanks!
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:TitleViewSample"
x:Class="TitleViewSample.MainPage">
<NavigationPage.TitleView>
<Switch x:Name="Switch1" IsToggled="True" Margin="5,0,0,0"/>
</NavigationPage.TitleView>
<ContentPage.Content>
<StackLayout>
<!-- Place new controls here -->
<Label Text="Welcome to Xamarin.Forms!"
HorizontalOptions="Center"
VerticalOptions="CenterAndExpand" />
</StackLayout>
</ContentPage.Content>
</ContentPage>
I don't think it is possible. You can, however, take the same approach I did when I needed to customise my title bar.
First, you hide the navigation bar, then you can specify your bar at the top, and use the controls you would like.

Resources