原代码:
IdImage.source = new BitMapImage(new Uri(filePath));
filePath更新后,再次调用该方法,IdImage.source未更新
原因:
BitMapImage 仍在占用上一资源,filePath无法删除文件进行更新
修改:
//将文件转为byte
string filePath = "filePath";
BinaryReader binReader = new BinaryReader(File.Open(filePath, FileMode.Open));
FileInfo fileInfo = new FileInfo(filePath);
byte[] bytes = binReader.ReadBytes((int)fileInfo.Length);
binReader.Close();
// 获取资源后立马关闭
BitmapImage bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.StreamSource = new MemoryStream(bytes);
bitmap.EndInit();
//释放BitmapImage资源后 ,获取BitmapImage 为IdImage赋值
IdImage.Source = bitmap;