我试图从文件系统上保存的文件加载一些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);
}
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,有时Uris作为相对文件Uris加载,有时它们被装载为相对Pack Uris.似乎没有任何押韵或理由,哪个会以哪种方式加载.有时我会以一种方式加载所有Uris,或只加载一对,或大部分,并且每次运行代码时它都会改变.
有什么想法在这里发生了什么?