How to remove or hide Toolbar item in specific page error: System.IndexOutOfRangeException: Index was outside the bounds of the array
How to remove or hide Toolbar item in specific page error: System.IndexOutOfRangeException: Index was outside the bounds of the array
我正在尝试 Remove()
或 Clear()
ToolbarItems
。这是我在 MainPage.cs
中创建 ToolbarItem
的代码
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.
如您所见,此项目仅会在 iOS 后消失。
这是我的页面 class 我想 Remove()
这些 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.
}
}
}
在这个 class 中,我尝试了两种方法,但 none 有效。感谢您的回答或建议。
这里的问题是 PPoPage
是与 MainPage
不同的页面实例,因此包含它自己的 ToolbarItems 集合。
尝试用main.ToolbarItems.Clear()
代替this.ToolbarItems.Clear()
,假设main
属性已经初始化,或者你可以用[=15访问顶级当前页面=].
我的任务是更改工具栏项的可见性,我创建了包含 ToolbarItems
集合包装器的基本内容页面。我的示例可以帮助您:
some "tutorial" that I've posted on CodeReview
首先,我真的很想感谢所有的答案和建议,我试图在我的解决方案中实现这两个答案,但不幸的是没有成功。我没有关注 class 的主要原因是我实现了整个 iOS 工具栏,它在我的 iOS 项目中。
在 iOS 工具栏上,我只是写了简单的 if 语句:
var currentPage = ((NavigationPage)Element).CurrentPage;
if (currentPage != null && currentPage is ContentPage)
{
TopViewController.NavigationItem.RightBarButtonItems = new UIBarButtonItem[0];
return;
}
我正在尝试 Remove()
或 Clear()
ToolbarItems
。这是我在 MainPage.cs
ToolbarItem
的代码
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.
如您所见,此项目仅会在 iOS 后消失。
这是我的页面 class 我想 Remove()
这些 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.
}
}
}
在这个 class 中,我尝试了两种方法,但 none 有效。感谢您的回答或建议。
这里的问题是 PPoPage
是与 MainPage
不同的页面实例,因此包含它自己的 ToolbarItems 集合。
尝试用main.ToolbarItems.Clear()
代替this.ToolbarItems.Clear()
,假设main
属性已经初始化,或者你可以用[=15访问顶级当前页面=].
我的任务是更改工具栏项的可见性,我创建了包含 ToolbarItems
集合包装器的基本内容页面。我的示例可以帮助您:
some "tutorial" that I've posted on CodeReview
首先,我真的很想感谢所有的答案和建议,我试图在我的解决方案中实现这两个答案,但不幸的是没有成功。我没有关注 class 的主要原因是我实现了整个 iOS 工具栏,它在我的 iOS 项目中。
在 iOS 工具栏上,我只是写了简单的 if 语句:
var currentPage = ((NavigationPage)Element).CurrentPage;
if (currentPage != null && currentPage is ContentPage)
{
TopViewController.NavigationItem.RightBarButtonItems = new UIBarButtonItem[0];
return;
}