【发布时间】:2016-06-07 07:56:38
【问题描述】:
我使用ObservableCollection<BarcodeInfo> 作为ItemsSource 的ListView 来生成ViewCells。一个 Cell 包含 2 个Labels 和一个ZXingBarcodeImageView,绑定到我的BarcodeInfo-class,一切都按预期工作。
现在我要从ListView 中删除几个单元格,但是一旦我尝试这样做,我就会从ZXingBarcodeImageView 中得到以下异常
System.ArgumentException: 发现空内容
这是我的 XAML
<ListView RowHeight="50">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="3*" />
</Grid.ColumnDefinitions>
<zxing:ZXingBarcodeImageView
BarcodeFormat="{Binding Format}"
BarcodeOptions="{Binding Options}"
BarcodeValue="{Binding Text}"
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand"
Margin="5"
Grid.Column="0"/>
<StackLayout Grid.Row="0" Grid.Column="1"
Spacing="0" VerticalOptions="CenterAndExpand">
<Label Text="{Binding Text}"
LineBreakMode="TailTruncation"
VerticalOptions="End"/>
<Label Text="{Binding Format}"
VerticalOptions="End"
LineBreakMode="TailTruncation"/>
</StackLayout>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
ListView 中ObservableCollection<BarcodeInfo> _barcodeCollection; 的类
public class BarcodeInfo
{
public string Text
{ get; set; }
public string Detail
{ get; set; }
public BarcodeFormat Format
{ get; set; }
public EncodingOptions Options
{ get; set; }
}
我一尝试异常就会发生
_barcodeCollection.RemoveAt(i);
我已经实现了INotifyPropertyChanged 并尝试将所有属性设置为 null ,这可以正常工作,但是 ZXingBarcodeImageView 没有清除条形码图像,如果我尝试从集合中删除项目,仍然会抛出异常.我现在已经没有更多的想法了。
我希望有人可以帮助我。
更新
因为i 似乎令人困惑,这是我正在使用的循环
for (int i = 0; i < _barcodeCollection.Count; i++)
{
var response =
await _serverUrl.PostUrlEncodedAsync(
new { barcode = _barcodeCollection[i].Text })
.ReceiveString();
if (string.Equals(response, "ok", StringComparison.OrdinalIgnoreCase))
{
percentage += progressSteps;
_barcodeCollection.RemoveAt(i); //EXCEPTION!!!
i--; // index must be checked twice else one element will be skipped
await UploadProgress.ProgressTo(percentage, 250, Easing.Linear);
}
}
【问题讨论】:
-
你的
i是什么?当您删除了所有元素并且您仍试图从集合中删除某些内容时,不是这种情况吗? -
i是 for 循环的一部分for(int i = 0; i < _barcodeCollection.Count; i++)里面是我检查的内容,如果为真,我会尝试删除元素 -
不要在
RemoveAt(i)之后增加i。 -
@Clemens 我不知道。我检查过,索引始终是我需要的索引,并且在集合中仍然可用。索引
i处的对象也永远不会为空 -
你不在 for 循环的
i++部分?您可能应该将该循环的代码添加到您的问题中,以便我们了解您实际在做什么。
标签: c# xaml xamarin xamarin.forms zxing