WP7应用开发笔记 继承BitmapSource并使用独立存储来缓存远程的图片
2012-02-28 23:05
585 查看
作为Web App访问远程图片是经常的遇到功能,Wp本身提供了Image 很好的支持通过图片的Uri显示图片
public ImageSource Source { get; set; }
<Image Source="https://www.google.com/intl/en_com/images/srpr/logo3w.png" />
为了减少网络流量,需要将图片缓存到本地数据存储中。复习一下WP的本地数据存储:
设置:使用 IsolatedStorageSettings 类将数据存储为键/值对。
文件和文件夹:使用 IsolatedStorageFile 类存储文件和文件夹。
本地数据库:7.1新增,只能支持LINQ TO SQL ,不能写SQL语句。
WP提供了一个叫IsolatedStorageFileStream的Stream和FileStream操作一样
Uri uriSource = new Uri(@”https://www.google.com/intl/en_com/images/srpr/logo3w.png”);
image1.ImageSource = new StorageCachedImage(uriSource);
用IsoStoreSpy查看(这里是我APP实际的图)
![](https://oscdn.geek-share.com/Uploads/Images/Content/202004/03/9fb02658043032cd6b60b9d65c111429.jpg)
public ImageSource Source { get; set; }
<Image Source="https://www.google.com/intl/en_com/images/srpr/logo3w.png" />
为了减少网络流量,需要将图片缓存到本地数据存储中。复习一下WP的本地数据存储:
Windows Phone 本地数据存储
Windows Phone 应用程序可以使用独立存储将数据储存到手机本地。应用程序可以通过三种方式储存数据:设置:使用 IsolatedStorageSettings 类将数据存储为键/值对。
文件和文件夹:使用 IsolatedStorageFile 类存储文件和文件夹。
本地数据库:7.1新增,只能支持LINQ TO SQL ,不能写SQL语句。
本地存储图片
首先图片应该选择IsolatedStorageFile来存储:WP提供了一个叫IsolatedStorageFileStream的Stream和FileStream操作一样
using (var fileStream = isolatedStorageFile.OpenFile(filePath, FileMode.OpenOrCreate, FileAccess.Write)) { stream.CopyTo(fileStream); }
缓存的思路
View Codeusing System; using System.Diagnostics; using System.IO; using System.IO.IsolatedStorage; using System.Net; using System.Windows.Media.Imaging; namespace KimiStudio.Controls { /// <summary> /// 独立存储缓存的图片源 /// </summary> public sealed class StorageCachedImage : BitmapSource { private readonly Uri uriSource; private readonly string filePath; private const string CacheDirectory = "CachedImages"; static StorageCachedImage() { //创建缓存目录 using (var isolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication()) { if (!isolatedStorageFile.DirectoryExists(CacheDirectory)) { isolatedStorageFile.CreateDirectory(CacheDirectory); } } } /// <summary> /// 创建一个独立存储缓存的图片源 /// </summary> /// <param name="uriSource"></param> public StorageCachedImage(Uri uriSource) { this.uriSource = uriSource; //文件路径 filePath = Path.Combine(CacheDirectory, uriSource.AbsolutePath.TrimStart('/').Replace('/', '_')); OpenCatchSource(); } /// <summary> /// 打开缓存源 /// </summary> private void OpenCatchSource() { bool exist; using (var isolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication()) { exist = isolatedStorageFile.FileExists(filePath); } if (exist) { SetCacheStreamSource(); } else { SetWebStreamSource(); } } /// <summary> /// 设置缓存流到图片 /// </summary> private void SetCacheStreamSource() { using (var isolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication()) using (var stream = isolatedStorageFile.OpenFile(filePath, FileMode.Open, FileAccess.Read)) { SetSource(stream); } } /// <summary> /// 下载Uri中的图片 /// </summary> private void SetWebStreamSource() { var httpWebRequest = (HttpWebRequest)WebRequest.Create(uriSource); httpWebRequest.AllowReadStreamBuffering = true; httpWebRequest.BeginGetResponse(ResponseCallBack, httpWebRequest); } /// <summary> /// 下载回调 /// </summary> /// <param name="asyncResult"></param> private void ResponseCallBack(IAsyncResult asyncResult) { var httpWebRequest = asyncResult.AsyncState as HttpWebRequest; if(httpWebRequest == null)return; try { var response = httpWebRequest.EndGetResponse(asyncResult); using(var stream = response.GetResponseStream()) using (var isolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication()) using (var fileStream = isolatedStorageFile.OpenFile (filePath, FileMode.OpenOrCreate, FileAccess.Write)) { stream.CopyTo(fileStream); } Dispatcher.BeginInvoke(SetCacheStreamSource); } catch(Exception err) { Debug.WriteLine(err.Message); } } } }
测试
定义一个ImageUri uriSource = new Uri(@”https://www.google.com/intl/en_com/images/srpr/logo3w.png”);
image1.ImageSource = new StorageCachedImage(uriSource);
用IsoStoreSpy查看(这里是我APP实际的图)
![](https://oscdn.geek-share.com/Uploads/Images/Content/202004/03/9fb02658043032cd6b60b9d65c111429.jpg)
相关文章推荐
- 继承BitmapSource并使用独立存储来缓存远程的图片
- WP7应用开发笔记-插曲 小心使用MessageBox
- 学习WP7应用开发的笔记--在App和Page中使用资源集合的注意点1
- Android 应用开发 之使用LruCache和DiskLruCache来在内存和SD卡中缓存图片
- Android 应用开发 之使用LruCache和DiskLruCache来在内存和SD卡中缓存图片
- WP7应用开发笔记-技巧 使用VisualState布置屏幕方向处理
- WP7应用开发笔记(14) 使用Caliburn Micro简化MVVM
- WP7应用开发笔记-豆知识 使用本地值的时候应该多考虑使用系统主题资源
- Android 应用开发 之使用LruCache和DiskLruCache来在内存和SD卡中缓存图片
- 使用Google Map Api在Android平台上开发地图应用3
- Android网络图片加载缓存处理库的使用---第三方库学习笔记(五)
- iOS开发之opencv学习笔记四:使用feature2d识别图片
- 使用C#开发Metro 风格应用的路线图 -- 移植wp7应用到metro上
- Android 自定义View 使用ShapeDrawable加BitmapShader实现图片局部放大效果
- iOS开发笔记--使用blend改变图片颜色
- iOS开发笔记--使用blend改变图片颜色
- 【Cocos2d-X开发学习笔记】第27期:游戏背景之贴图地图类(CCTileMapAtlas)的使用
- android应用开发揭秘examples_04-09笔记(Spinner的使用,略有修改)
- Android应用开发学习笔记之使用Bundle交换数据
- 微软企业库4.1学习笔记(十四)缓存模块2 使用缓存模块进行开发