WP_从独立存储区读取缓存的图片
///<summary>
/// 独立存储缓存的图片源
/// 用法:item.img = new StorageCachedImage(newUri(http://www.baidu.com/12.jpg));
///</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;
string sUrl = uriSource.AbsolutePath;
//文件路径
filePath = Path.Combine(CacheDirectory, sUrl.Substring(sUrl.LastIndexOf("/") + 1, sUrl.Length - sUrl.LastIndexOf("/") - 1));
OpenCatchSource();
}
///<summary>
/// 打开缓存源
///</summary>
private void OpenCatchSource()
{
//网络可用时,下载图片(网络不可用时读取本地缓存)
if (CommonConst.CheckNetWorking())
{
SetWebStreamSource();
}
else
{
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))
{
CopyStream(stream, fileStream);
}
Dispatcher.BeginInvoke(SetCacheStreamSource);
}
catch (Exception err)
{
//Debug.WriteLine(err.Message);
}
}
private static void CopyStream(System.IO.Stream input, IsolatedStorageFileStream output)
{
byte[] buffer = new byte[32768];
long TempPos = input.Position;
int readCount;
do
{
readCount = input.Read(buffer, 0, buffer.Length);
if (readCount > 0)
{
output.Write(buffer, 0, readCount);
}
} while (readCount > 0);
input.Position = TempPos;
}
}
WP_从独立存储区读取缓存的图片的更多相关文章
- C#从证书存储区读取证书
using System; using System.Collections.Generic; using System.Linq; using System.Security.Cryptograph ...
- WP开发图片保存到独立存储并从独立存储中读取
需要添加引用命名空间 using System.IO; using System.IO.IsolatedStorage; 1.将图片保存到独立存储空间 using (IsolatedStorageFi ...
- 调用EF的存储过程报“存储区数据提供程序返回的数据读取器所具有的列数对于所请求的查询不够”问题
在运用Entity Framework调用存储过程的时候,遇到"调用EF的存储过程报"调用EF的存储过程报“存储区数据提供程序返回的数据读取器所具有的列数对于所请求的查询不够”问题 ...
- 第21章 DMA—直接存储区访问
本章参考资料:<STM32F76xxx参考手册>DMA控制器章节. 学习本章时,配合<STM32F76xxx参考手册>DMA控制器章节一起阅读,效果会更佳,特别是涉及到寄存器说 ...
- 第21章 DMA—直接存储区访问—零死角玩转STM32-F429系列
第21章 DMA—直接存储区访问 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.com/fi ...
- DMA—直接存储区访问
本章参考资料:< STM32F4xx 中文参考手册> DMA 控制器章节.学习本章时,配合< STM32F4xx 中文参考手册> DMA 控制器章节一起阅读,效果会更佳,特别是 ...
- PHP 开发 APP 接口 学习笔记与总结 - APP 接口实例 [3] 首页 APP 接口开发方案 ② 读取缓存方式
以静态缓存为例. 修改 file.php line:11 去掉 path 参数(方便),加上缓存时间参数: public function cacheData($k,$v = '',$cacheTim ...
- 转:内存区划分、内存分配、常量存储区、堆、栈、自由存储区、全局区[C++][内存管理][转载]
内存区划分.内存分配.常量存储区.堆.栈.自由存储区.全局区[C++][内存管理][转载] 一. 在c中分为这几个存储区1.栈 - 由编译器自动分配释放2.堆 - 一般由程序员分配释放,若程序员不释放 ...
- WP8 独立存储 总结3(应用设置)
•可在独立存储中使用ApplicationSettings对象•在独立存储中存储键/值对的Dictionary方式存储 •存储的对象将永久保存 在应用设置中保存数据 void saveString(s ...
随机推荐
- IGS_学习笔记08_IREP通过soapUI测试客户化Web Service调用(案例)
20150819 Created By BaoXinjian
- SQL语句的Select部分只写必要的列
如果Select部分包含不需要的列,这会强制DB2必须进入数据页来得到所请求的特定列,这就要求更多的I/O操作.另外,如果再对这个不需要的列进行排序,就需要创建和传递一个更大的排序文件,相应地会使排序 ...
- 使ViewStub 来提高UI的加载的性能
首先看下API中的ViewStub 根据的文档的说明,ViewStub是一种默认不可见的试图,它没有大小,所以不能被改变,也不能通过某些把viewstub添加到布局当中来, 不过我们可以使用infla ...
- JAVA 边界布局管理器
//边界布局管理器 import java.awt.*; import javax.swing.*; public class Jiemian1 extends JFrame{ //定义组件 JBut ...
- [原创博文] 用Python做统计分析 (Scipy.stats的文档)
[转自] 用Python做统计分析 (Scipy.stats的文档) 对scipy.stats的详细介绍: 这个文档说了以下内容,对python如何做统计分析感兴趣的人可以看看,毕竟Python的库也 ...
- mongodb 查询使用
> db.jd_58tc_raw.findOne() { "_id" : "2659e4e4caf0504ec4362478e2ed57ca", &quo ...
- iOS设计模式反思之单例模式的进化
什么是单例模式? 单例模式想一个大独裁者,他规定在他的国度里面,所有数据的访问和请求都得经过他,甚至你要调用相关的函数也得经过它.学术一点就是,单例模式,为某一类 需求和数据提供了统一的程序接口.主要 ...
- jsLint配置参数解释
转自: http://www.cnblogs.com/elementstorm/archive/2013/04/10/3012679.htmlanon :true //匿名函数声明中function关 ...
- WebService中实现上传下载文件
不多说,直接看代码: /*上传文件的WebService*/ using System; using System.Collections; using System.Collections.Gene ...
- Collection集合List、Set
Collection集合,用来保存一组数据的数据结构. Collection是一个接口,定义了所有集合都应该包含的特征和行为 Collection派生出了两类集合 List和Set List接口:Li ...