LongListSelector 出现异常。急!
异常:
{System.ArgumentException: Value does not fall within the expected range.}
{System.Collections.ListDictionaryInternal}
这个发生在我逐条删除LongListSelector数据是发生的, MainLongListSelector.ItemsSource 是和ObservableCollection绑定以便动态删除,可是当我每次删除到8,9条时就会发生如上异常。
我的xmal:
<phone:LongListSelector x:Name="MainLongListSelector" Margin="0,0,-12,0" ItemsSource="{Binding DuplicateContacts}">
<phone:LongListSelector.ItemTemplate>
<DataTemplate>
<StackPanel>
<Grid>
<TextBlock Text="{Binding DisplayName}"
Foreground="Black"
TextTrimming="WordEllipsis"
Margin="30,10,50,10"
VerticalAlignment="Center"
Style="{StaticResource PhoneTextLargeStyle}"/>
<Button x:Name="Detail"
HorizontalAlignment="Right" BorderThickness="0"
VerticalAlignment="Bottom"
Margin="0,10,20,10"
Click="Detail_Click">
<Button.Background>
<ImageBrush Stretch="Uniform" ImageSource="/Images/rightwards.png"/>
</Button.Background>
</Button>
</Grid>
<toolkit:ContextMenuService.ContextMenu>
<toolkit:ContextMenu IsZoomEnabled="True">
<toolkit:MenuItem Header="Delete" CommandParameter="{Binding}"
Click="MenuItem_Click"/>
</toolkit:ContextMenu>
</toolkit:ContextMenuService.ContextMenu>
</StackPanel>
</DataTemplate>
</phone:LongListSelector.ItemTemplate>
</phone:LongListSelector>
这是一个BUG,如果你从顶部并且速度很快的删除就会这样,从LongListSelector的底部开始删除就不会
那该如何解决呢?
这个帖子http://social.msdn.microsoft.com/Forums/wpapps/en-US/bec4efbf-269b-4be0-aacc-99c8be59b861/wp8-longlistselector-exception-when-removing-items?forum=wpdevelop
说的是什么解决办法,我看不太懂?
不是给出代码了嘛
public class LongListSelectorEx : LongListSelector
{
protected override Size MeasureOverride(Size availableSize)
{
try
{
return base.MeasureOverride(availableSize);
}
catch (ArgumentException)
{
return base.MeasureOverride(availableSize);
}
}
}
继承一下这个类,屏蔽这个异常。