C# 字典 键不存在
本文关键字:不存在 字典 | 更新日期: 2023-09-27 17:55:29
我正在创建一个简单的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时,会出现所描述的错误
您这里的问题是,您正在填充搜索实例中包含的字典,但您正在查找播放器实例中包含的字典中的数据,该词典仍然为空。
每次创建从 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)
{
}
}