How to hide navigation Toolbar icon in xamarin?

11,134

Solution 1

I would suggest to build a bindable ToolBoxItem. That way you can control the visibility through a view model property.

An implementation could look like that:

public class BindableToolbarItem : ToolbarItem
{
    public static readonly BindableProperty IsVisibleProperty = BindableProperty.Create(nameof(IsVisible), typeof(bool), typeof(BindableToolbarItem), true, BindingMode.TwoWay, propertyChanged: OnIsVisibleChanged);

    public bool IsVisible
    {
        get => (bool)GetValue(IsVisibleProperty);
        set => SetValue(IsVisibleProperty, value);
    }

    private static void OnIsVisibleChanged(BindableObject bindable, object oldvalue, object newvalue)
    {
        var item = bindable as BindableToolbarItem;

        if (item == null || item.Parent == null)
            return;

        var toolbarItems = ((ContentPage)item.Parent).ToolbarItems;

        if ((bool)newvalue && !toolbarItems.Contains(item))
        {
            Device.BeginInvokeOnMainThread(() => { toolbarItems.Add(item); });
        }
        else if (!(bool)newvalue && toolbarItems.Contains(item))
        {
            Device.BeginInvokeOnMainThread(() => { toolbarItems.Remove(item); });
        }
    }
}

Solution 2

As you have discovered yourself there is not IsVisible. So you will have to implement functionality like that yourself if you still want it.

Another way would be to handle it in the pages' code-behind and remove or add the toolbar item whenever needed.

Adding and removing is simple, just add and remove items to the ToolbarItems collection: ToolbarItems.RemoveAt(0); for instance will remove the first toolbar item.

Solution 3

Putting @Gerald answer in action, it would be done this way:

void Done_Clicked(System.Object sender, System.EventArgs e)
{
    //Do somthing and hide the done item
    ShowDoneToolbarItem(false, (ToolbarItem)sender);
}

void Entry_Focused(System.Object sender, Xamarin.Forms.FocusEventArgs e)
{
    //Show the done item
    ShowDoneToolbarItem(true);
}

void ShowDoneToolbarItem(bool show, ToolbarItem item = null)
{
    if(show)
    {
        ToolbarItem done = new ToolbarItem();
        done.Text = "Done";
        done.Clicked += Done_Clicked;
        ToolbarItems.Add(done);
    }
    else if(item != null)
    {
        ToolbarItems.Remove(item);
    }
}

This is cleaner and works from the code behind.

Share:
11,134
Maulik shah
Author by

Maulik shah

ios developer

Updated on June 05, 2022

Comments

  • Maulik shah
    Maulik shah almost 2 years

    I want to hide navigation bar button in xamarin. how can i do that using binding. Toolbar item doesn't have "IsVisible" property.

    Following is my xaml code

    Xaml code of navigation bar button

    please help me to sort out this issue.

  • Gerald Versluis
    Gerald Versluis almost 6 years
    Updated my answer a bit. It isn't rocket science and not even Xamarin specific. ToolbarItems is just an array that you can access.
  • Gerald Versluis
    Gerald Versluis almost 6 years
    I'm sorry, I don't know what you mean by that
  • Maulik shah
    Maulik shah almost 6 years
    i have create x:name="toolbaritem" in xaml and then access cs file but there is no property like "RemoveAt".
  • Maulik shah
    Maulik shah almost 6 years
    how can i hide toolbar item from model.in model i get a webservice response then i want to hide edit button according to response
  • Maulik shah
    Maulik shah almost 6 years
    I want to hide toolbar button using binding like isvisable ={BInding showItems}.
  • Gerald Versluis
    Gerald Versluis almost 6 years
    That is not possible unfortunately
  • Maulik shah
    Maulik shah almost 6 years
    how can i hide toolbar item from model.in model i get a webservice response then i want to hide edit button according to response
  • Legion
    Legion almost 4 years
    Thank you very much!
  • thomasgalliker
    thomasgalliker about 3 years
    This is a very nice solution. However, keep in mind that the default visibility is "True" which means, you can toggle it using a binding like IsVisible="{Binding ShowToolbarItem}" from visible to hidden. With this approach, you cannot have the toolbar item hidden from the very beginning of the page lifetime (at least not with some flickering).
  • Nuri YILMAZ
    Nuri YILMAZ over 2 years
    Question; Why Xamarin team didn't implement IsVisible property?
  • Christoph Mett
    Christoph Mett over 2 years
    With this approach I pretty often get an InvalidOperationException stating "collection was modified; enumeration operation may not execute" at Page.OnBindingContextChanged. This seems to be the case because in Page.OnBindingContextChanged there is a foreach-loop iterating through the pages ToolbarItems and setting the new BindingContext which seems to interfere with the above approach to remove/add the ToolbarItems. Anyone has an idea how to fix that?
  • Christoph Mett
    Christoph Mett over 2 years
    Okay, I fixed it by making it async and adding a Task.Delay before doing the adding/removing of the item.