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 ...
随机推荐
- curl 同时发送多个请求
// 创建一对cURL资源 $ch1 = curl_init(); $ch2 = curl_init(); // 设置URL和相应的选项 curl_setopt($ch1, CURLOPT_URL, ...
- socket 发送发送HTTP请求
socket方式: $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); //socket_set_option($socket, SOL_S ...
- python (11)文件的读写 按行读文件
读文件: 读取文件 f = open('\info.txt') fil = f.read() f.close() 按行读文件: f = open("info.txt") while ...
- PDF内容不允许复制的解决方法!
PDF文档的内容不允许复制解决方法! PDF的加密有两个级别: 一个是Owner级别,就是打开文档时需要输入密码PDF Password Remover可以用来破解Owner级别的口令,但是不能用于破 ...
- You need tcl 8.5 or newer in order to run the Redis test
安装Redis时候make test出现的 , 不用下什么tcl8.5再tar了 , 直接yum install tcl就好.
- Git的撤消操作 - 重置, 签出 和 撤消(转载)
From:http://gitbook.liuhui998.com/4_9.html http://ihower.tw/blog/archives/2622 相较于SVN这种commit就推送到远端伺 ...
- SPR EAD NET 6
SPR EAD_NET6 下载地址 http://www.gcpowertools.com.cn/downloads/trial/Spread.NET/EN_SPREAD_NET6_SETUP_RA_ ...
- [ActionScript 3.0] AS3 时间格式化方法
/** * 格式化时间,格式 00:00:00 * @param total 总时间(毫秒) */ function getFormatTime(total:uint):String { if (to ...
- js解析或获取页面路径归纳
/** * 当填写参数href后,解析你给的参数,如果为空自动从获取浏览器的地址 *测试路径:>>>http://127.0.0.1:8020/url/index.html?id=1 ...
- [Uva 10085] The most distant state (BFS)
题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...