我想从网上加载图片,但要缓存它们,然后从本地存储文件夹中显示给用户。
我有以下IValueConverter类:
public class ImageToIsolatedPathConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var fileName = (string)value;
if (NetworkInterface.GetIsNetworkAvailable())
{
LoadImage(fileName);
}
try
{
var file = Path.GetFileName(fileName);
// TODO code to retrieve file and return it
}
catch (Exception ex)
{
}
return fileName;
}
}
LoadImage(..)
如下:
private void LoadImage(string imagePath)
{
var imageFileName = Path.GetFileName(imagePath);
var webClient = new WebClient();
webClient.OpenReadCompleted += async (s1, e1) =>
{
if (e1.Error == null)
{
try
{
bool isSpaceAvailable = IsSpaceIsAvailable(e1.Result.Length);
if (isSpaceAvailable)
{
// TODO save file using ApplicationData.Current.LocalFolder
using (var isfs = new IsolatedStorageFileStream(imageFileName,
FileMode.OpenOrCreate,
IsolatedStorageFile.GetUserStoreForApplication()))
{
long fileLen = e1.Result.Length;
byte[] b = new byte[fileLen];
e1.Result.Read(b, 0, b.Length);
isfs.Write(b, 0, b.Length);
isfs.Flush();
}
}
else
{
// not enough space
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
else
{
MessageBox.Show(e1.Error.Message);
}
};
webClient.OpenReadAsync(new Uri(imagePath));
}
如何使用ApplicationData.Current.LocalFolder
中的WebClient
保存文件,然后从同一StorageFolder
读取文件?
我需要为Windows Phone 8和Windows 8应用程序执行此操作。
有什么想法吗?