我正在尝试Remove()
或Clear()
ToolbarItems
。这是我ToolbarItem
在 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);
}
}
}
编辑:当我包含
this.ToolbarItems.Clear();
在OpenWindow
初始化另一个页面打开的方法中并且它可以工作时!清除所有工具栏项,但不幸的是显示此错误:System.IndexOutOfRangeException: Index was outside the bounds of the array.
如您所见,此项目应仅在 iOS 中消失。这是我想要的页面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.
}
}
}
在这堂课中,我尝试了这两种方法,但都没有奏效。感谢您的回答或建议。