2013-01-22 75 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