集合中的每个图像都有一个序列化的文件路径。在加载集合时,我需要从文件路径加载图像。下面的代码将无法工作,因为IsolatedStorageFileStream与用于to image.SetSource()的IRandomAccessStream不兼容。
public BitmapImage Image
{
get
{
var image = new BitmapImage();
if (FilePath == null) return null;
IsolatedStorageFileStream stream = new IsolatedStorageFileStream(FilePath, FileMode.Open, FileAccess.Read, IsolatedStorageFile.GetUserStoreForApplication());
image.SetSource(stream);
return image;
}
}是否有替代代码来完成这一任务?
发布于 2015-12-28 21:33:18
您只需使用WindowsRuntimeStreamExtensions.AsRandomAccessStream扩展方法:
using System.IO;
...
using (var stream = new IsolatedStorageFileStream(
FilePath, FileMode.Open, FileAccess.Read,
IsolatedStorageFile.GetUserStoreForApplication()))
{
await image.SetSourceAsync(stream.AsRandomAccessStream());
}当我测试这个SetSource时,它阻塞了应用程序,所以我使用了SetSourceAsync。
您还可以直接访问隔离存储文件夹,如下所示:
var file = await ApplicationData.Current.LocalFolder.CreateFileAsync(
FilePath, CreationCollisionOption.OpenIfExists);
using (var stream = await file.OpenReadAsync())
{
await image.SetSourceAsync(stream);
}https://stackoverflow.com/questions/34499548
复制相似问题