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_学习笔记01_Integrated SOA Gateway介绍(案例)
2014-01-01 Created By BaoXinjian
- PLSQL_性能优化效能跟踪工具SQL Trace分析(案例)
2014-06-25 Created By BaoXinjian
- HDU Count the string+Next数组测试函数
链接:http://www.cnblogs.com/jackge/archive/2013/04/20/3032942.html 题意:给定一字符串,求它所有的前缀出现的次数的和.这题很纠结,一开始不 ...
- POJ 1269 Intersecting Lines(计算几何)
题意:给定4个点的坐标,前2个点是一条线,后2个点是另一条线,求这两条线的关系,如果相交,就输出交点. 题解:先判断是否共线,我用的是叉积的性质,用了2遍就可以判断4个点是否共线了,在用斜率判断是否平 ...
- openmp并行计算
#include <omp.h>#include <stdio.h>#include <stdlib.h> void test(int n){ for (int i ...
- 转--Android学习笔记-实用代码合集
http://blog.csdn.net/yf210yf/article/details/7295577 转载请注明原文出处:奔跑的蜗牛(袁方的技术博客)点击打开链接 一.当利用textview显示内 ...
- java获取任意日期
现有两个办法 1:Date as = new Date(new Date().getTime()-24*60*60*1000); SimpleDateFormat matter1 = new Sim ...
- JMeter中3种参数值的传递
小伙伴们在使用JMeter的过程中,肯定会遇到参数值传递的问题,下面来和大家总结下,在使用JMeter做压力测试的时候,常见的3种参数值的传递是怎样的. (一)从CSV文件读取要批量输入的变量 假如我 ...
- TRUNCATE与 DELETE
源地址:http://zhidao.baidu.com/link?url=9zB64BuXiAXNPF-zxvd6VLGTKb2FsUzQ-FsRAeQaYzycOGT5uGPXb-oB44TuYoP ...
- 使用maven构建一个helloworld maven项目
1.下载maven http://maven.apache.org/ 选择一个较新版本下下来 2.配置m2_home环境变量 先解压,新建用户变量m2_home,将bin目录添加到path变量中 3. ...