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();
inOpenWindow
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With