-3

我正在创建一个简单的 mp3 播放器,只是为了看看 Dictionary 是如何工作的。我的项目中有 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 时,出现描述的错误

4

1 回答 1

0

您的问题是您正在填充 Search 实例中包含的 Dictionary ,但您正在查找 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)
    {

    }
}
于 2013-01-22T15:16:33.763 回答