ObservableCollection からアイテムを削除しているときに、特定の数のアイテムを削除した後、「値が期待される範囲内にありません」という引数例外が発生します。
コードは次のとおりです。
public abstract class BaseList<T> : ObservableCollection<T>
{
}
public class Videos
{
//Contains members and methods
}
public class VideoList:BaseList<Videos>
{
//Contains members and methods
}
//Singleton class
public class VideoPlaylist
{
private static VideoPlaylist _instance;
public static VideoPlaylist Instance
{
get
{
if (_instance == null)
{
_instance = new VideoPlaylist();
}
return _instance;
}
}
private VideoList _myPlaylist = new VideoList();
public VideoList MyPlaylist
{
get { return _myPlaylist; }
set
{
if (_myPlaylist != value)
{
_myPlaylist = value;
}
}
}
public void RemoveFromPlayList(string id)
{
if (MyPlaylist != null)
{
for (int index = MyPlaylist.Count - 1; index >= 0; index--)
{
Videos video = MyPlaylist.ElementAt(index);
if (video.Id == id)
{
MyPlaylist.RemoveAt(index);
}
}
}
}
}
xaml:
<phone:PhoneApplicationPage.Resources>
<DataTemplate x:Name="VideoItemListTemplate">
<Grid MinHeight="100"
Margin="0,0">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<!--Content-->
<Grid Grid.Row="0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<!--Image-->
<Grid Grid.Column="0">
<Image Stretch="UniformToFill"
Grid.Column="0"
Height="150"
Width="150">
<Image.Source>
<BitmapImage UriSource="{Binding Image}"
CreateOptions="BackgroundCreation" />
</Image.Source>
</Image>
</Grid>
<!--Description and Publish Date-->
<StackPanel
Grid.Column="1">
<TextBlock Text="{Binding Title}"
TextAlignment="Left"
TextWrapping="Wrap"
MaxHeight="100"
Margin="12,0,0,0"
FontSize="30"
Style="{StaticResource RangeelaTextBlockStyle}"
TextTrimming="WordEllipsis"
VerticalAlignment="Top" />
<Button Content="Remove" Foreground="#bd8326"
BorderBrush="#bd8326" Click="Remove_Clicked"
FontWeight="Thin" FontSize="25"
BorderThickness="0.8"/>
</StackPanel>
</Grid>
<!--Line Seperator-->
<!--<Rectangle Grid.Row="1" Height="1"
Stroke="White"
StrokeThickness="0.6" Opacity="0.4"
Margin="0,12" />-->
</Grid>
</DataTemplate>
</phone:PhoneApplicationPage.Resources>
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<TextBlock HorizontalAlignment="Center"
VerticalAlignment="Center"
Foreground="White"
x:Name="EmptyPlaylistMessage"
Text="Playlist is empty"
Visibility="Collapsed"/>
<ProgressBar x:Name="MyProgressBar"
Visibility="Collapsed"
IsIndeterminate="False" />
<phone:LongListSelector x:Name="LatestVideoPivotItem"
LayoutMode="List"
GridCellSize="210,250"
Margin="12,12,12,0"
Tap="Videos_Tapped"
ItemTemplate="{StaticResource VideoItemListTemplate}"
ItemsSource="{Binding}"/>
</Grid>
MainPage のメソッド RemoveClicked:
private void Remove_Clicked(object sender, RoutedEventArgs e)
{
Button image = sender as Button;
Videos video = image.DataContext as Videos;
_IsAddToPlaylistSelected = true;
if (video != null)
{
VideoPlaylist.Instance.RemoveFromPlayList(video.Id);
}
}
メイン ページから、Singleton クラスのインスタンスを使用してアイテムを削除しようとしました。