我正在玩一个音乐播放器应用程序,并将播放列表存储在列表中。 这显示在LongListSelector中,其中包含带有图像和两个文本块的StackPanel:
<DataTemplate x:Key="playlistItemTemplate"> <StackPanel Orientation="Horizontal"> <Image Height="80" Width="80" Margin="0,0,10,0" Source="/Assets/stop.png" Tap="removeSong_Tap" /> <StackPanel VerticalAlignment="Center" Orientation="Horizontal" > <StackPanel Orientation="Vertical"> <TextBlock Text="{Binding Name}" FontSize="22" VerticalAlignment="Center" HorizontalAlignment="Left" /> <TextBlock Text="{Binding Artist}" Style="{StaticResource PhoneTextSubtleStyle}" VerticalAlignment="Center" HorizontalAlignment="Left" /> </StackPanel> </StackPanel> </StackPanel> </DataTemplate>当用户点击图像时,我想从列表中删除所选歌曲。 我使用以下代码:
private void removeSong_Tap(object sender, System.Windows.Input.GestureEventArgs e) { int selectedIndex = playlistList.ItemsSource.IndexOf(playlistList.SelectedItem as ItemViewModel); if (selectedIndex == Data.currentSongNo) { if (Data.currentSongNo == Data.playList.Count - 1) //last song in the playlist { MediaPlayer.Stop(); } else { playNextSong(); } removeSongFromPlaylist(selectedIndex); } }但每次单击图像时,SelectedItem都会显示为null,表示selectedIndex设置为-1
我也试过用:
int selectedIndex = App.ViewModel.Items.IndexOf(playlistList.SelectedItem as ItemViewModel);但这有相同的结果。
我有什么想法我做错了吗?
I am playing around with a music player app and have the playlist stored in a List. This is displayed in a LongListSelector containing a StackPanel with an image and two text blocks:
<DataTemplate x:Key="playlistItemTemplate"> <StackPanel Orientation="Horizontal"> <Image Height="80" Width="80" Margin="0,0,10,0" Source="/Assets/stop.png" Tap="removeSong_Tap" /> <StackPanel VerticalAlignment="Center" Orientation="Horizontal" > <StackPanel Orientation="Vertical"> <TextBlock Text="{Binding Name}" FontSize="22" VerticalAlignment="Center" HorizontalAlignment="Left" /> <TextBlock Text="{Binding Artist}" Style="{StaticResource PhoneTextSubtleStyle}" VerticalAlignment="Center" HorizontalAlignment="Left" /> </StackPanel> </StackPanel> </StackPanel> </DataTemplate>When the user clicks on the image, I want to remove the selected song from the list. I am using the following code:
private void removeSong_Tap(object sender, System.Windows.Input.GestureEventArgs e) { int selectedIndex = playlistList.ItemsSource.IndexOf(playlistList.SelectedItem as ItemViewModel); if (selectedIndex == Data.currentSongNo) { if (Data.currentSongNo == Data.playList.Count - 1) //last song in the playlist { MediaPlayer.Stop(); } else { playNextSong(); } removeSongFromPlaylist(selectedIndex); } }But every time I click on the image, the SelectedItem comes up as null, meaning the selectedIndex is set to -1
I've also tried using:
int selectedIndex = App.ViewModel.Items.IndexOf(playlistList.SelectedItem as ItemViewModel);But this has the same results.
Any ideas what I'm doing wrong?
最满意答案
我确定有几种方法可以做到这一点。 我实现这一点的一个简单方法是使用在SelectionChanged事件开始时检查的标志。
tap事件将在树中的selection_changed事件之前触发,因此我设置了一个Tap事件,如下所示:
private void ItemClose_Tap(object sender, System.Windows.Input.GestureEventArgs e) { isItemCloseTapped = true; }然后,当Selection Changed事件触发时,我实现如下:
private void MusicListSelectChange(object sender, SelectionChangedEventArgs e) { Model.MusicItem item = ((LongListSelector)sender).SelectedItem as Model.MusicItem; if (item == null) return; if (isItemCloseTapped ) { CloseInList(item); isItemCloseTapped = false; } //... //((LongListSelector)sender).SelectedItem = null; )同样,这是一种方法,但它在我的结束时运作良好。
There are a few ways to do this I'm sure. One simple way I've implemented this is to use a flag that's checked at the start of the SelectionChanged event.
The tap event will fire before the selection_changed event in the tree, so I've set up a Tap event like this:
private void ItemClose_Tap(object sender, System.Windows.Input.GestureEventArgs e) { isItemCloseTapped = true; }Then, as the Selection Changed event fires, I've implemented as follows:
private void MusicListSelectChange(object sender, SelectionChangedEventArgs e) { Model.MusicItem item = ((LongListSelector)sender).SelectedItem as Model.MusicItem; if (item == null) return; if (isItemCloseTapped ) { CloseInList(item); isItemCloseTapped = false; } //... //((LongListSelector)sender).SelectedItem = null; )Again, this is one way to do it, but it's worked well on my end.
更多推荐
selectedIndex,SelectedItem,image,电脑培训,计算机培训,IT培训"/> <meta name=&q
发布评论