我正在尝试从文件系统上保存的文件中加载一些 BitmapImages。我有一个键和相对文件路径的字典。不幸的是,Uri 构造函数在加载图像的方式上似乎是不确定的。
这是我的代码:
foreach(KeyValuePair<string, string> imageLocation in _imageLocations)
{
try
{
BitmapImage img = new BitmapImage();
img.BeginInit();
img.UriSource = new Uri(@imageLocation.Value, UriKind.Relative);
img.EndInit();
_images.Add(imageLocation.Key, img);
}
catch (Exception ex)
{
logger.Error("Error attempting to load image", ex);
}
}
不幸的是,有时 Uris 会作为相对文件 Uris 加载,有时它们会作为相对 Pack Uris 加载。似乎没有任何押韵或理由说明哪个会以哪种方式加载。有时我会以一种方式加载所有的 Uris,或者只加载几个,或者大多数,每次运行代码时它都会改变。
有什么想法吗?