我有一个字符串列表,当我删除一个字符串时,字符串的索引不会改变,因此当我尝试删除另一个索引高于我得到的当前数字并且错误说索引超出范围时。
public class MyClass{
public StackLayout SavedHoursLayout = new StackLayout {};
public Label RemoveHoursLabel;
public TapGestureRecognizer RemoveTapped;
public Grid HoursRemoveGrid;
public Button AddHoursButton = new Button();
public MyClass()
{
Content = new StackLayout
{
Children = { AddHoursButton,SavedHoursLayout }
}
AddHoursButton.Clicked+=AddHoursButton_Clicked;
AddSavedHours();
}
public void AddSavedHours()
{
Label Time = new Label { };
RemoveHoursLabel = new Label { Text="remove",TextColor=Color.Red,FontAttributes=FontAttributes.Italic};
HoursRemoveGrid = new Grid();
RemoveTapped = new TapGestureRecognizer();
this.BindingContext = HoursRemoveGrid;
HoursRemoveGrid.Children.Add(Time,0,0);
HoursRemoveGrid.Children.Add(RemoveHoursLabel,1,0);
SavedHoursLayout.Children.Add(HoursRemoveGrid);
RemoveHoursLabel.GestureRecognizers.Add(RemoveTapped);
RemoveTapped.Tapped += RemoveTapped_Tapped;
void RemoveTapped_Tapped(object sender, EventArgs e)
{
int position = SavedHoursLayout.Children.IndexOf(HoursRemoveGrid);
SavedHoursLayout.Children.RemoveAt(position);
}
}
private void AddHoursButton_Clicked(object sender, System.EventArgs e)
{
AddSavedHours();
}
}
问题
在我将子项添加到后SavedHoursLayout,然后单击RemoveHoursLabel它会删除当前RemoveHoursLabel但其余的索引保持不变因此当我单击另一个时它会删除分配给它的索引的子项,如果索引超出范围,我收到一条错误消息
索引超出范围,不应为负数或高于项目数。
那么当一个孩子被删除时,我如何将孩子的索引更新为当前的索引SavedHoursLayout。
呼如林
心有法竹
随时随地看视频慕课网APP
相关分类