gpt4 book ai didi

c# - 如何删除或隐藏特定页面中的工具栏项错误 : System. IndexOutOfRangeException:索引超出数组范围

转载 作者:可可西里 更新时间:2023-11-01 07:48:10 26 4
gpt4 key购买 nike

我正在尝试 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 中消失。这是我的页面类,我想在其中 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.
}
}
}

在这门课上,我尝试了两种方法,但都没有用。感谢您的回答或建议。

最佳答案

我的任务是更改工具栏项的可见性,我创建了包含 ToolbarItems 集合包装器的基本内容页面。我的示例可以帮助您:

sample on git

some "tutorial" that I've posted on CodeReview

enter image description here

关于c# - 如何删除或隐藏特定页面中的工具栏项错误 : System. IndexOutOfRangeException:索引超出数组范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39295685/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com