试图弄清楚为什么这不起作用,列表是使用组合框项目(将本地HDD的根地址列为项目)检索照片,当选择该项目时,它将被转换为字符串并且应该用作GetFiles
方法的路径,但在运行时拧紧(字符串路径=)行,我得到“对象引用未设置为对象的实例”非常感谢如果有人可以告诉我出了什么问题
public List<Photos> LoadImages ///List Retrieves and Loads Photos
{
get
{
List<Photos> Image = new List<Photos>();
string path = HDDSelectionBox.SelectedItem.ToString(); //ComboBox SelectedItem Converted To String As Path
foreach (string filename in Directory.GetFiles(path, "*jpg"))
{
try
{
Image.Add( //Add To List
new Photos(
new BitmapImage(
new Uri(filename)),
System.IO.Path.GetFileNameWithoutExtension(filename)));
}
catch { } //Skips Any Image That Isn't Image/Cant Be Loaded
}
return Image;
}
}
答案 0 :(得分:0)
您应该将.
放在行中的文件扩展名之前:
Directory.GetFiles(path, "*.jpg")
您还需要检查HDDSelectionBox.SelectedItem
是否为空:
public List<Photos> LoadImages ///List Retrieves and Loads Photos
{
get
{
List<Photos> images = new List<Photos>();
if (HDDSelectionBox.SelectedItem != null)
{
string path = HDDSelectionBox.SelectedItem.ToString(); //ComboBox SelectedItem Converted To String As Path
foreach (string filename in Directory.GetFiles(path, "*.jpg"))
{
try
{
images.Add( //Add To List
new Photos(
new BitmapImage(
new Uri(filename)),
System.IO.Path.GetFileNameWithoutExtension(filename)));
}
catch { } //Skips Any Image That Isn't Image/Cant Be Loaded
}
}
return images;
}
}
此外,这可能更适合于方法而不是属性,因为它会进行大量处理......