MediaPlaybackList.Items.IndexOf(_currentItem)无效!! [英] MediaPlaybackList.Items.IndexOf(_currentItem) is not working !!
问题描述
我正在开发一个音乐播放器。我需要从MediaPlaybackList获取特定项的索引。但是,MediaPlaybackList.Items.IndexOf(_currentItem)始终返回-1。为什么?我需要帮助。
MediaPlaybackItem _currentMediaPlaybackItem = new MediaPlaybackItem(MediaSource.CreateFromStorageFile(song.SongFile));
MediaPlaybackList playbacklist = new MediaPlaybackList();
if(MusicPlayedFrom ==" MyMusicPage")
{
CurrentlyPlayingSong = song;
foreach(歌曲中的var项目)
{
MediaPlaybackItem playbackItem = new MediaPlaybackItem(MediaSource.CreateFromStorageFile(item.SongFile));
playbacklist.Items.Add(playbackItem);
}
}
MediaPlayer.Source = playbacklist;
var count = playbacklist.Items.Count;
var index = playbacklist.Items.IndexOf(_currentMediaPlaybackItem);
>>但是,MediaPlaybackList.Items.IndexOf(_currentItem)始终返回-1。为什么?我需要帮助。
这种行为的原因是_currentMediaPlaybackItem对象不是你在列表中添加
的exactMediaPlaybackItem对象。
_currentMediaPlaybackItem对象,我们将其称为
A,是从目标文件创建的新对象。
然后您在for循环中创建了一个MediaPlaybackItem列表。在此列表
中,有一个从目标文件创建的新MediaPlaybackItem对象。我们称之为
B 。
虽然 A 和
B 都是从相同的文件,但它们是两个不同的对象。
A 不在列表中, B 会添加到列表中。因此,当您搜索
A 时,它返回-1表示该项目在列表中不存在。(
IList< T> .IndexOf(T)
方法 )
最好的问候,
罗伊跨度>
I am developing a music player. I need to get the index for a specific item from the MediaPlaybackList. But MediaPlaybackList.Items.IndexOf(_currentItem) always return -1. Why ?? I need help.
MediaPlaybackItem _currentMediaPlaybackItem = new MediaPlaybackItem(MediaSource.CreateFromStorageFile(song.SongFile)); MediaPlaybackList playbacklist = new MediaPlaybackList(); if (MusicPlayedFrom == "MyMusicPage") { CurrentlyPlayingSong = song; foreach (var item in Songs) { MediaPlaybackItem playbackItem = new MediaPlaybackItem(MediaSource.CreateFromStorageFile(item.SongFile)); playbacklist.Items.Add(playbackItem); } } MediaPlayer.Source = playbacklist; var count = playbacklist.Items.Count; var index = playbacklist.Items.IndexOf(_currentMediaPlaybackItem);
>> But MediaPlaybackList.Items.IndexOf(_currentItem) always return -1. Why ?? I need help.
The reason for this behavior is that the _currentMediaPlaybackItem object is not the exactlyMediaPlaybackItem object you added in the list.
The _currentMediaPlaybackItem object, we call it as A, is a new object created from the target file.
Then you have created a list of MediaPlaybackItems in a for loop. In this list there is a new MediaPlaybackItem object created from the target file. Let’s call it B.
Although A and B are all created from the same file but they are two different object. A is not in the list and B is added to the list. So when you search for A , it return -1 that means the item doesn’t exist in the list.( IList<T>.IndexOf(T) Method)
Best regards,
Roy
这篇关于MediaPlaybackList.Items.IndexOf(_currentItem)无效!!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!