我正在使用一个转换幻灯片控件,它有一个可观察的字符串集合绑定到itemsource。这些字符串是幻灯片中每张图片的文件路径。当我第一次加载WPF应用程序时,它正确地运行了此方法(使用目录路径生成PicSlideShowCollection):
public void SelectImages(string path)
{
// Validate
if (string.IsNullOrEmpty(path)) throw new ArgumentException("path");
PicSlideShowCollection.Clear();
// Get directory info for specified path
DirectoryInfo di = new DirectoryInfo(path);
// Image mask
string[] extensions = new string[] { "*.jpg", "*.png", "*.gif", "*.bmp" };
// Search for all
foreach (string extension in extensions)
{
foreach (FileInfo fi in di.GetFiles(extension.ToLower()))
{
PicSlideShowCollection.Add(fi.FullName);
}
}
}
然而,我有一个按钮,允许用户更改幻灯片中使用的图像目录并重新运行上述方法。当执行该操作时,我会收到以下错误消息:
这发生在PicSlideShowCollection.Clear()指令上。如果我注释掉该指令,则新目录中的图像将被添加到原始目录图片中,这不是我想要的。传递给Remove的GeneratorPosition“-1,1”没有偏移量等于0。
我知道这与PicSlideShowCollection作为幻灯片控件的项源有关,但我需要知道如何防止出现此错误。
谢谢!