LongListSelector SelectedItem为Null(LongListSelector SelectedItem is Null)

编程入门 行业动态 更新时间:2024-10-24 10:24:22
LongListSelector SelectedItem为Null(LongListSelector SelectedItem is Null)

我正在玩一个音乐播放器应用程序,并将播放列表存储在列表中。 这显示在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

本文发布于:2023-08-01 20:46:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1362856.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:LongListSelector   SelectedItem   Null

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!