我有一个字符串列表,当我删除一个字符串时,字符串的索引不会改变,因此当我尝试删除另一个索引高于我得到的当前数字并且错误说索引超出范围时。 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。
1 回答
心有法竹
TA贡献1866条经验 获得超5个赞
使用 sender 获取您希望删除的当前网格:
void RemoveTapped_Tapped(object sender, EventArgs e)
{
var grid = (sender as Label).Parent as Grid;
int position = SavedHoursLayout.Children.IndexOf(grid);
SavedHoursLayout.Children.RemoveAt(position);
}
- 1 回答
- 0 关注
- 107 浏览
添加回答
举报
0/150
提交
取消
