0

基本上我正在制作一个点唱机,我对 WMPLib 库有一个大问题。
我正在尝试从播放列表中删除一个项目,但问题是我找不到任何方法来按名称获取索引或实际媒体。
我正在考虑制作一个播放列表的副本数组,但这只是愚蠢的更多工作,而不是我应该投入到这样一个简单的任务中。

 private void queue_listbox_SelectedIndexChanged(object sender, System.EventArgs e)
    {
        if (queue_listbox.SelectedItem != null)
        {
            wplayer.currentPlaylist.removeItem('Insert code here');
            music_listbox.Items.Add(queue_listbox.SelectedItem);
            queue_listbox.Items.Remove(queue_listbox.SelectedItem);
        }
    }
4

1 回答 1

0

只需使用Dictionary<TKey, TValue>这样的:

// The string will be used as the name to find the song. 
// I don't know what type you are using for the song, 
// so I'm just using the madeup type "Song"
Dictionary<string, Song> songs = new Dictionary<string, Song>();

然后你可以通过做(伪代码)来删除它

// Removes "Look at this Photograph" from the songs Dictionary
songs["Look at this Photograph - Nickleback"].Remove();

字典几乎是列表,几乎是数组,除了传递数字作为索引器(songs[5]获取第 6 首歌曲)之外,您可以传递任何类型TKeysongs["Look at this Photograph"]获取与该字符串配对的歌曲)。

我不知道您是否制作了该Listbox课程,或者它是否由 Microsoft 构建。如果它是构建的,那么Items如果它已经是一个数组,那么它就不能成为字典,在这种情况下,你需要变得更有创意。您可能必须基本上重新创建Listbox才能获得Dictionary或一些聪明的解决方法。

你可以做一些事情,比如制作一个ListboxManager可以容纳 a 的东西,Dictionary<Listbox, Dictionary<string, Song>>并使用一个名为的扩展方法GetItems(this Listbox listbox) => ListBoxManager.Listboxes[listbox];,它可能会起作用。

于 2018-05-07T01:30:58.010 回答