MediaPlaybackList.Items.IndexOf(_currentItem)无效!! [英] MediaPlaybackList.Items.IndexOf(_currentItem) is not working !!

查看:131
本文介绍了MediaPlaybackList.Items.IndexOf(_currentItem)无效!!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个音乐播放器。我需要从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);

解决方案

嗨SHAKIR820,


>>但是,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);

解决方案

Hi SHAKIR820,

>> 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆