我正在创建一个简单的MP3播放器,只是为了看看字典是如何工作的。我的项目中有6个课程,其中2个课程无关紧要。第一类包括标签,第二类包括搜索,第三类是播放,第四类是抽象类,包括字典。搜索和播放类都继承自第四类。
当我去搜索时,对象已成功添加到字典Dictionary dict<string, Tags>
,并且标签是从Tags类添加的。
我的问题是,当我尝试从Play类到达从字典中获取路径时,它表示密钥不存在。
代码为dict[playSongName].Path;
,其中playSongName是用于存储在字典中的歌曲的名称。
class Player : Liste
{
public void Play(string playSongName)
{
currentSongPath = dictPesmi[playSongName].Path; // Error here, key was not found
currentSongName = playSongName;
media.Source = new Uri(currentSongPath);
media.Play();
}
}
abstract class Liste
{
public Dictionary<string, Tagi> dictPesmi = new Dictionary<string, Tagi>();
public Id3TagClass id3tagi = new Id3TagClass();
}
class Search : Liste
{
string[] filesFound;
const string extension = "*.mp3";
public void Find(string searchPath)
{
if (Directory.Exists(searchPath))
{
filesFound = Directory.GetFiles(searchPath, extension, SearchOption.AllDirectories);
foreach (string filePath in filesFound)
{
string[] filePathSplit = filePath.Split('\\');
string fileName = filePathSplit[filePathSplit.Length - 1];
dictPesmi[fileName] = new Tagi() { Path = filePath, Name = fileName };
dictPesmi[fileName].Author = id3tagi.GetArtist(filePath);
dictPesmi[fileName].Album = id3tagi.GetAlbum(filePath);
dictPesmi[fileName].Title = id3tagi.GetTitle(filePath);
System.Windows.MessageBox.Show(dictPesmi[fileName].Path);
}
}
}
}
class Tagi
{
string path;
string name;
string title;
string author;
string album;
public string Album
{
get { return album; }
set { album = value; }
}
public string Author
{
get { return author; }
set { author = value; }
}
public string Title
{
get { return title; }
set { title = value; }
}
public string Name
{
get { return name; }
set { name = value; }
}
public string Path
{
get { return path; }
set { path = value; }
}
}
主要课程
public partial class MainWindow : Window
{
Player player = new Player();
Search search = new Search();
// string selectedSong;
public MainWindow()
{
InitializeComponent();
}
public void KeyDownEvent_(object sender, KeyEventArgs e)
{
if (e.Key == Key.F1)
player.Play(lbPlaylist.Items[lbPlaylist.Items.IndexOf(lbPlaylist.SelectedItem)].ToString());// passed string
if (e.Key == Key.F3)
search.Find(tbSearch.Text);
if (e.Key == Key.F5)
{
foreach (Tagi d in search.dictPesmi.Values)
{
if (!lbPlaylist.Items.Contains(d.Name))
lbPlaylist.Items.Add(d.Name);
}
}
if (e.Key == Key.F9)
{
}
}
当我按F1时,所描述的错误出现
答案 0 :(得分:0)
您的问题在于您正在填写搜索实例中包含的词典,但您正在查找Player实例中包含的数据,该数据仍为空。
每次创建从Liste派生的类的实例时,都会创建一个新的空字典。事情可以修改你的代码如下,但这不是真正的面向对象,我认为它真的“丑陋”,我会改变你的应用程序的设计:
public void KeyDownEvent_(object sender, KeyEventArgs e)
{
if (e.Key == Key.F1)
player.Play(lbPlaylist.Items[lbPlaylist.Items.IndexOf(lbPlaylist.SelectedItem)].ToString());// passed string
if (e.Key == Key.F3)
search.Find(tbSearch.Text);
if (e.Key == Key.F5)
{
foreach (Tagi d in search.dictPesmi.Values)
{
if (!lbPlaylist.Items.Contains(d.Name))
lbPlaylist.Items.Add(d.Name);
}
// add this line to use the data just collected:
player.dictPesmi = search.dictPesmi;
}
if (e.Key == Key.F9)
{
}
}