C# 从独立存储(Windows Phone)保存和加载图像
我在这个链接上发现了一个非常有用的类:-它帮助我建立缓存图像的逻辑。但在我的情况下,我有:C# 从独立存储(Windows Phone)保存和加载图像,c#,silverlight,windows-phone-7,silverlight-4.0,C#,Silverlight,Windows Phone 7,Silverlight 4.0,我在这个链接上发现了一个非常有用的类:-它帮助我建立缓存图像的逻辑。但在我的情况下,我有: private void DetailView_SelectionChanged(object sender, SelectionChangedEventArgs e) { SaveAndLoadImage(feedItem); } 在这种方法中,我保存并加载来自独立存储的图像。但
private void DetailView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
SaveAndLoadImage(feedItem);
}
在这种方法中,我保存并加载来自独立存储的图像。但由于某些权限(在IsolatedStorageFileStream上不允许操作),我无法立即加载文件。如何更正逻辑以立即保存和加载图像
public void SaveAndLoadImage(MediaItemViewModel curItem)
{
string url = string.Empty;
if (!string.IsNullOrEmpty(curItem.ThumbUrl))
{
url = curItem.ThumbUrl;
}
if ((string.IsNullOrEmpty(curItem.ThumbUrl)) && (!string.IsNullOrEmpty(curItem.MediaUrl)))
{
url = curItem.MediaUrl;
}
if ((!string.IsNullOrEmpty(url)) && (CacheImageFile.GetInstance().IsOnStorage(new Uri(url)) == false))
{
CacheImageFile.DownloadFromWeb(new Uri(url));
}
curItem.ImageSource = CacheImageFile.ExtractFromLocalStorage(new Uri(url)) as BitmapImage;
}
看看下面的链接 用于从独立存储加载图像。使用流
BitmapImage bi = new BitmapImage();
using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile("logo.jpg", FileMode.Open, FileAccess.Read))
{
bi.SetSource(fileStream);
this.img.Height = bi.PixelHeight;
this.img.Width = bi.PixelWidth;
}
}
this.img.Source = bi;
看看下面的链接 用于从独立存储加载图像。使用流
BitmapImage bi = new BitmapImage();
using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile("logo.jpg", FileMode.Open, FileAccess.Read))
{
bi.SetSource(fileStream);
this.img.Height = bi.PixelHeight;
this.img.Width = bi.PixelWidth;
}
}
this.img.Source = bi;
您正在从web异步获取图像,但立即转到下一行读取尚未写入独立存储的文件。这就是导致您出现异常的原因 您可以尝试使用ManualResetEvent编辑在github上找到的缓存库。请注意,您必须在另一个线程上进行方法调用 例如:
public class CacheImageFileConverter : IValueConverter
{
...
private static ManualResetEvent mre = new ManualResetEvent(true);
private static object DownloadFromWeb(Uri imageFileUri)
{
mre.Reset();
WebClient m_webClient = new WebClient(); //Load from internet
BitmapImage bm = new BitmapImage();
m_webClient.OpenReadCompleted += (o, e) =>
{
if (e.Error != null || e.Cancelled) return;
WriteToIsolatedStorage(IsolatedStorageFile.GetUserStoreForApplication(), e.Result, GetFileNameInIsolatedStorage(imageFileUri));
bm.SetSource(e.Result);
e.Result.Close();
mre.Set();
};
m_webClient.OpenReadAsync(imageFileUri);
return bm;
}
private static object ExtractFromLocalStorage(Uri imageFileUri)
{
mre.WaitOne();
string isolatedStoragePath = GetFileNameInIsolatedStorage(imageFileUri); //Load from local storage
using (var sourceFile = _storage.OpenFile(isolatedStoragePath, FileMode.Open, FileAccess.Read))
{
BitmapImage bm = new BitmapImage();
bm.SetSource(sourceFile);
return bm;
}
}
.... other methods
}
请注意使用Reset、Set和WaitOne发出信号。您从web异步获取图像,但立即转到下一行读取尚未写入独立存储的文件。这就是导致您出现异常的原因 您可以尝试使用ManualResetEvent编辑在github上找到的缓存库。请注意,您必须在另一个线程上进行方法调用 例如:
public class CacheImageFileConverter : IValueConverter
{
...
private static ManualResetEvent mre = new ManualResetEvent(true);
private static object DownloadFromWeb(Uri imageFileUri)
{
mre.Reset();
WebClient m_webClient = new WebClient(); //Load from internet
BitmapImage bm = new BitmapImage();
m_webClient.OpenReadCompleted += (o, e) =>
{
if (e.Error != null || e.Cancelled) return;
WriteToIsolatedStorage(IsolatedStorageFile.GetUserStoreForApplication(), e.Result, GetFileNameInIsolatedStorage(imageFileUri));
bm.SetSource(e.Result);
e.Result.Close();
mre.Set();
};
m_webClient.OpenReadAsync(imageFileUri);
return bm;
}
private static object ExtractFromLocalStorage(Uri imageFileUri)
{
mre.WaitOne();
string isolatedStoragePath = GetFileNameInIsolatedStorage(imageFileUri); //Load from local storage
using (var sourceFile = _storage.OpenFile(isolatedStoragePath, FileMode.Open, FileAccess.Read))
{
BitmapImage bm = new BitmapImage();
bm.SetSource(sourceFile);
return bm;
}
}
.... other methods
}
请注意在信令中使用了Reset、Set和WaitOne。您可以使用,我为应用程序创建了它,在应用程序中,我们需要加载、缓存和显示大量徽标、图标等
它可以用作绑定转换器,所以您甚至不应该更改代码!只需更新你的XAML
请退房,你会喜欢的;)
功能:
- 磁盘缓存
- 内存缓存
- 完全异步
- 可用作绑定转换器或通过编程从代码中获取
- 完全开源,分叉和改进它李>
<Image Source="{Binding ImageUrl, Converter={StaticResource MyAppJetImageLoaderConverter}}"/>
另外,很抱歉,我复制了另一个问题的答案,但windows phone上的图像缓存是一个巨大的问题,我想分享我的解决方案,这样每个人都可以使用它并为开发者社区改进。你可以使用,我为应用程序创建了它,我们需要加载、缓存和显示大量的徽标、图标等
它可以用作绑定转换器,所以您甚至不应该更改代码!只需更新你的XAML
请退房,你会喜欢的;)
功能:
- 磁盘缓存
- 内存缓存
- 完全异步
- 可用作绑定转换器或通过编程从代码中获取
- 完全开源,分叉和改进它李>
<Image Source="{Binding ImageUrl, Converter={StaticResource MyAppJetImageLoaderConverter}}"/>
另外,很抱歉,我复制了其他问题的答案,但windows phone上的图像缓存是一个巨大的问题,我想分享我的解决方案,因此,每个人都可以使用它并改进开发者社区在保存图像后不能立即加载图像-在IsolatedStorageFileStream上不允许执行操作。在保存图像后不能立即加载图像-在IsolatedStorageFileStream上不允许执行操作。谢谢,但在我给您的链接中也是这样逻辑)谢谢,但在我给你的链接中,我的逻辑是一样的)谢谢,但它在我的情况下不起作用-应用程序挂起你能检查一下这行是否被调用过吗?mre.Set();你能运行SaveAndLoadImage(feedItem)方法吗;在另一个线程上(后台工作线程、新线程,随便你喜欢什么)谢谢,但在我的情况下它不起作用-应用程序挂起。你能检查一下是否调用过这一行吗?mre.Set();你能运行SaveAndLoadImage(feedItem)方法吗;在另一个线程(后台工作线程、新线程,无论您喜欢什么)上,我正在使用windows phone 8.1(通用应用程序),您的JetImageLoader与此不兼容请解释您在
JetImageLoader
的GitHub repo上的错误我正在使用windows phone 8.1(通用应用程序)您的JetImageLoader与此不兼容请解释您在JetImageLoader的GitHub repo上的错误