2016-06-07 86 views
0

我使用ObservableCollection<BarcodeInfo>作为ItemsSourceListView生成ViewCells。一个细胞包含2 Labels和一个ZXingBarcodeImageView与我的BarcodeInfo -class绑定,一切都按预期工作。ZXingBarcodeImageView从ObservableCollection移除时出现异常

现在我已经从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> 

而在该ListVi的班ObservableCollection<BarcodeInfo> _barcodeCollection; EW

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不清除条形码-Image和异常仍然抛出,如果我尝试从集合中删除该项目。我处于一个没有更多想法的地步。

我希望有人能帮助我。

更新 因为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); 
    } 
} 
+0

你的“我”是什么?当你删除了所有的元素,并且你仍然试图从集合中删除某些东西时,情况并非如此。 –

+0

'i'是for循环的一部分for(int i = 0; i <_barcodeCollection.Count; i ++)'里面是我检查的东西,如果是的话我试着去掉元素 –

+0

不要''' RemoveAt移除(ⅰ)'。 – Clemens

回答

0

我已成功地找出问题的原因,它的方式的副作用是当元素被移除并且ListViewCachingStrategy被设置为RetainElement(默认)时,ListView被刷新。 当从集合中删除项目并且ZXing自定义渲染器中的regenerate()方法崩溃时,会调用自定义渲染器OnElementPropertyChanged。 这可以通过将ListViewCachingStrategy设置为RecycleElement解决,这将与Android一起工作,但是iOS 10仍然存在问题,它不能正确支持ListViewCachingStrategy设置为RecycleElement的列表视图,iOS 10当前仅适用于ListViewCachingStrategy设置为

public class MyZXingBarcodeImageView : ZXingBarcodeImageView 
    { 
    //THERE IS NO CODE REQUIRED HERE 
    } 

,然后根据斑马线源上创建自己的自定义渲染,但具有下列替换无效再生()方法:RetainElement,这可以,如果你在你的便携代码例如,创建一个自定义的控制来应对:

void regenerate() 
    { 
     if(formsView != null && !string.IsNullOrWhiteSpace(formsView.BarcodeValue)) 
     { 
      var writer = new ZXing.Mobile.BarcodeWriter(); 

      if(formsView != null && formsView.BarcodeOptions != null) 
       writer.Options = formsView.BarcodeOptions; 
      if(formsView != null && formsView.BarcodeFormat != null) 
       writer.Format = formsView.BarcodeFormat; 

      var value = formsView != null ? formsView.BarcodeValue : string.Empty; 

      Device.BeginInvokeOnMainThread(() => 
      { 

       var image = writer.Write(value); 

       imageView.Image = image; 

       }); 

     } 
    } 

的变化是在第一个if语句,从测试空字符串转换到string.IsNullOrWhiteSpace

您不必创建为Android定制呈现,如果你使用ListViewCachingStrategy设置为RecycleElement,为ZXingBarcodeImageView基地渲染器无误地使用。

相关问题