2013-01-22 74 views
-3

我正在创建一个简单的MP3播放器,以查看字典的工作原理。我在我的项目中有6个班,其中2个班不相关。第一类包括标签,第二类是搜索,第三类是游戏,第四类是抽象类,包括字典。搜索和播放类都从第四类继承。C#字典键不存在

当我去搜索时,对象已成功添加到字典Dictionary dict<string, 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所描述的错误显示出来

+1

嗨jonjohnson,你可以把相关代码所以我们可以看到你在做什么? –

+2

没有人可以帮助你没有代码.. :) – Wolf

+4

尝试写一个[简短,自包含,正确的例子](http://sscce.org/)来说明你的问题 - 否则几乎不可能诊断你的问题。也许你忘了修剪你的输入?也许你忘了比较区分大小写? – Adam

回答

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) 
    { 

    } 
} 
+0

谢谢!我将查找方法从搜索移到播放器并删除了类搜索。 – jonjohnson