Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove or hide Toolbar item in specific page error: System.IndexOutOfRangeException: Index was outside the bounds of the array

I am trying to Remove() or Clear() ToolbarItems. Here is my code where I am creating ToolbarItem in MainPage.cs

public partial class MainPage : MasterDetailPage
{
   public ToolbarItem cCounter = new ToolbarItem() { Icon = "picture.png" };
   public ToolbarItem pPo = new ToolbarItem() { Text = "-" };

   public MainPage()
    {
        InitializeComponent();
        if (Device.OS == TargetPlatform.iOS)
        {
            provider.Clicked += iOS_Ppo_Clicked;
            ToolbarItems.Add(cCounter);
            ToolbarItems.Add(pPo);
        }
    }

    private void iOS_Ppo_Clicked(object sender, EventArgs e)
    { 
        OpenWindow();            
    }

    public async void OpenWindow()
    {
        if (await Common.WindowComands.CanOpenWindow<PPoPage>(Detail))
        {  
            page = new PPoPage(Page3.Allproviders);

            this.ToolbarItems.Clear(); // here I am getting error:Index was outside the bounds of the array 

            page.OnSelected += Page_OnSelected;
            await Detail.Navigation.PushAsync(page, false);             
        }   
    }
}

Edit: when I included this.ToolbarItems.Clear(); in OpenWindow method which initialise that another page opens and it works! Cleans all toolbar items but unfortunately shows this error:

System.IndexOutOfRangeException: Index was outside the bounds of the array.

This items should disappear only for iOS as you see. Here is my page class where I would like to Remove() these ToolbarItems:

public partial class PPoPage : ContentPage
{
   public MainPage main { get; set; }

   private List<PPo> Pro;

   public PPoPage(List<PPo> po)
   {
      InitializeComponent();
      if (Device.OS == TargetPlatform.iOS)
      {
         Pro = po;
         CreateLayout();
         // HERE I WANT TO REMOVE TOOLBARITEMS FOR THIS PAGE
         this.ToolbarItem.Remove(main.cCounter); // here there is error
         this.ToolbarItems.Clear(); // this also doesn't work, because toolbar items still exist after this initialization.
      }
   }
}

In this class I tried both approaches, but none work. Thank you for answers or suggestions.

like image 548
BinaryTie Avatar asked Sep 02 '16 15:09

BinaryTie


1 Answers

I had task to change visibility of toolbar item and I created base content page with wrapper around ToolbarItems collection. My sample could help you:

sample on git

some "tutorial" that I've posted on CodeReview

enter image description here

like image 192
Yehor Hromadskyi Avatar answered Oct 08 '22 01:10

Yehor Hromadskyi